Find how “smooth” a number is based on binary
$begingroup$
I haven't been on PP&CG for a while, so I thought I would post something!
Your task is to find how "smooth" a natural number is. Your method is to:
1: Convert the number to binary
2: Find the number of changes / switches
3: Find the length of the string (in binary)
4: Divide length by changes
So, an example. 5.
Starting at step one, we wind up with 101 for binary.
Step two is where we count the "switches". This is how many times a digit changes, so 100001 would count 2 switches. 101 counts 2, too.
Step three has a length of three in binary.
Step four gives us 3/2, or 1.5.
Doing this for 10 is also simple: Step one results with 1010, two with three, three with four, and a final result has 1.33333333... repeating.
If the inputs output infinity (examples: 1
, 3
, and 7
), you need to output something that tells you infinity, like $infty$ or Infinity
.
Now, you might ask, "what about scoring?" or something like that: You are scored in characters, so feel free to use a lot of code golfing languages. (I'm looking at you, Jelly)
If you round the output "smoothness factor" to 3 decimal places (4/3 is now 1.333, 5/3 is 1.667) your score is now x0.95, and being able to not only return a smoothness factor but also be able to compare two numbers (etc: putting in 5 and 10 returns > because 5's smoothness factor is greater than 10's) multiplies your value by x0.7. Command-line flags don't count for anything.
Have fun!
This challenge ends the 19th of March.
Current placeholders: The functional language holder, with 45 chars, Nahuel Fouilleul, and the code golf language holder, with 7 chars, Luis Mendo.
code-golf binary base-conversion
$endgroup$
|
show 4 more comments
$begingroup$
I haven't been on PP&CG for a while, so I thought I would post something!
Your task is to find how "smooth" a natural number is. Your method is to:
1: Convert the number to binary
2: Find the number of changes / switches
3: Find the length of the string (in binary)
4: Divide length by changes
So, an example. 5.
Starting at step one, we wind up with 101 for binary.
Step two is where we count the "switches". This is how many times a digit changes, so 100001 would count 2 switches. 101 counts 2, too.
Step three has a length of three in binary.
Step four gives us 3/2, or 1.5.
Doing this for 10 is also simple: Step one results with 1010, two with three, three with four, and a final result has 1.33333333... repeating.
If the inputs output infinity (examples: 1
, 3
, and 7
), you need to output something that tells you infinity, like $infty$ or Infinity
.
Now, you might ask, "what about scoring?" or something like that: You are scored in characters, so feel free to use a lot of code golfing languages. (I'm looking at you, Jelly)
If you round the output "smoothness factor" to 3 decimal places (4/3 is now 1.333, 5/3 is 1.667) your score is now x0.95, and being able to not only return a smoothness factor but also be able to compare two numbers (etc: putting in 5 and 10 returns > because 5's smoothness factor is greater than 10's) multiplies your value by x0.7. Command-line flags don't count for anything.
Have fun!
This challenge ends the 19th of March.
Current placeholders: The functional language holder, with 45 chars, Nahuel Fouilleul, and the code golf language holder, with 7 chars, Luis Mendo.
code-golf binary base-conversion
$endgroup$
4
$begingroup$
It would be a bit clearer if both examples were given separately. Also, I don't think the bonuses add much to the challenge.
$endgroup$
– Arnauld
10 hours ago
3
$begingroup$
welcome to PPCG! You've been around for a month or so, but I would suggest using The Sandbox to get feedback on your challenges before posting them.
$endgroup$
– Giuseppe
10 hours ago
2
$begingroup$
Inputs1
,3
,7
etc give infinity. Should we really output infinity for those?
$endgroup$
– Luis Mendo
10 hours ago
7
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
9 hours ago
6
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
9 hours ago
|
show 4 more comments
$begingroup$
I haven't been on PP&CG for a while, so I thought I would post something!
Your task is to find how "smooth" a natural number is. Your method is to:
1: Convert the number to binary
2: Find the number of changes / switches
3: Find the length of the string (in binary)
4: Divide length by changes
So, an example. 5.
Starting at step one, we wind up with 101 for binary.
Step two is where we count the "switches". This is how many times a digit changes, so 100001 would count 2 switches. 101 counts 2, too.
Step three has a length of three in binary.
Step four gives us 3/2, or 1.5.
Doing this for 10 is also simple: Step one results with 1010, two with three, three with four, and a final result has 1.33333333... repeating.
If the inputs output infinity (examples: 1
, 3
, and 7
), you need to output something that tells you infinity, like $infty$ or Infinity
.
Now, you might ask, "what about scoring?" or something like that: You are scored in characters, so feel free to use a lot of code golfing languages. (I'm looking at you, Jelly)
If you round the output "smoothness factor" to 3 decimal places (4/3 is now 1.333, 5/3 is 1.667) your score is now x0.95, and being able to not only return a smoothness factor but also be able to compare two numbers (etc: putting in 5 and 10 returns > because 5's smoothness factor is greater than 10's) multiplies your value by x0.7. Command-line flags don't count for anything.
Have fun!
This challenge ends the 19th of March.
Current placeholders: The functional language holder, with 45 chars, Nahuel Fouilleul, and the code golf language holder, with 7 chars, Luis Mendo.
code-golf binary base-conversion
$endgroup$
I haven't been on PP&CG for a while, so I thought I would post something!
Your task is to find how "smooth" a natural number is. Your method is to:
1: Convert the number to binary
2: Find the number of changes / switches
3: Find the length of the string (in binary)
4: Divide length by changes
So, an example. 5.
Starting at step one, we wind up with 101 for binary.
Step two is where we count the "switches". This is how many times a digit changes, so 100001 would count 2 switches. 101 counts 2, too.
Step three has a length of three in binary.
Step four gives us 3/2, or 1.5.
Doing this for 10 is also simple: Step one results with 1010, two with three, three with four, and a final result has 1.33333333... repeating.
If the inputs output infinity (examples: 1
, 3
, and 7
), you need to output something that tells you infinity, like $infty$ or Infinity
.
Now, you might ask, "what about scoring?" or something like that: You are scored in characters, so feel free to use a lot of code golfing languages. (I'm looking at you, Jelly)
If you round the output "smoothness factor" to 3 decimal places (4/3 is now 1.333, 5/3 is 1.667) your score is now x0.95, and being able to not only return a smoothness factor but also be able to compare two numbers (etc: putting in 5 and 10 returns > because 5's smoothness factor is greater than 10's) multiplies your value by x0.7. Command-line flags don't count for anything.
Have fun!
This challenge ends the 19th of March.
Current placeholders: The functional language holder, with 45 chars, Nahuel Fouilleul, and the code golf language holder, with 7 chars, Luis Mendo.
code-golf binary base-conversion
code-golf binary base-conversion
edited 7 hours ago
Ethan Slota
asked 10 hours ago
Ethan SlotaEthan Slota
153
153
4
$begingroup$
It would be a bit clearer if both examples were given separately. Also, I don't think the bonuses add much to the challenge.
$endgroup$
– Arnauld
10 hours ago
3
$begingroup$
welcome to PPCG! You've been around for a month or so, but I would suggest using The Sandbox to get feedback on your challenges before posting them.
$endgroup$
– Giuseppe
10 hours ago
2
$begingroup$
Inputs1
,3
,7
etc give infinity. Should we really output infinity for those?
$endgroup$
– Luis Mendo
10 hours ago
7
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
9 hours ago
6
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
9 hours ago
|
show 4 more comments
4
$begingroup$
It would be a bit clearer if both examples were given separately. Also, I don't think the bonuses add much to the challenge.
$endgroup$
– Arnauld
10 hours ago
3
$begingroup$
welcome to PPCG! You've been around for a month or so, but I would suggest using The Sandbox to get feedback on your challenges before posting them.
$endgroup$
– Giuseppe
10 hours ago
2
$begingroup$
Inputs1
,3
,7
etc give infinity. Should we really output infinity for those?
$endgroup$
– Luis Mendo
10 hours ago
7
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
9 hours ago
6
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
9 hours ago
4
4
$begingroup$
It would be a bit clearer if both examples were given separately. Also, I don't think the bonuses add much to the challenge.
$endgroup$
– Arnauld
10 hours ago
$begingroup$
It would be a bit clearer if both examples were given separately. Also, I don't think the bonuses add much to the challenge.
$endgroup$
– Arnauld
10 hours ago
3
3
$begingroup$
welcome to PPCG! You've been around for a month or so, but I would suggest using The Sandbox to get feedback on your challenges before posting them.
$endgroup$
– Giuseppe
10 hours ago
$begingroup$
welcome to PPCG! You've been around for a month or so, but I would suggest using The Sandbox to get feedback on your challenges before posting them.
$endgroup$
– Giuseppe
10 hours ago
2
2
$begingroup$
Inputs
1
, 3
, 7
etc give infinity. Should we really output infinity for those?$endgroup$
– Luis Mendo
10 hours ago
$begingroup$
Inputs
1
, 3
, 7
etc give infinity. Should we really output infinity for those?$endgroup$
– Luis Mendo
10 hours ago
7
7
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
9 hours ago
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
9 hours ago
6
6
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
9 hours ago
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
9 hours ago
|
show 4 more comments
10 Answers
10
active
oldest
votes
$begingroup$
Perl 5 (-p -Mbignum
), 45 bytes
$_=sprintf"%b",$_;$_=y///c/(s/(.)(?!1)//g-1)
TIO
$endgroup$
add a comment |
$begingroup$
MATL, 7 characters
Btnwdz/
Try it online!
Explanation
B % Convert to binary
t % Duplicate
n % Number of elements
w % Swap
d % Consecutive differences
z % Number of nonzeros
/ % Divide
$endgroup$
2
$begingroup$
Honestly, how it it possible to incorporate 7 ASCII characters to make a complicated program like the one I described. I don't understand code golf anymore.
$endgroup$
– Ethan Slota
10 hours ago
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
10 hours ago
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
9 hours ago
1
$begingroup$
@EthanSlota MATL is at least pretty easy to understand, since for the most part, each command is a MATLAB or Octave command and there's a parser built in Octave / MATLAB. This would translate to something like@(x,b=dec2bin(x))numel(b)/nnz(diff(b))
but it uses Luis' much terser language to achieve the same result.
$endgroup$
– Giuseppe
9 hours ago
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
9 hours ago
|
show 2 more comments
$begingroup$
Jelly, 8 bytes, score 8
Maybe there is a terser way... edit: I don't think there is.
BL÷BITLƲ
Infinity is given as inf
.
Try it online!
...other 8's are possible too, for example BµITL÷@L
or BL÷BnƝSƊ
.
$endgroup$
$begingroup$
Here’s a version to get the bonuses: Try it online! score 16 x 0.95 x 0.7 = 10.64
$endgroup$
– Nick Kennedy
8 hours ago
$begingroup$
Shouldn'tinf
be greater than all other values? If we can identifyinf
as less than all others we could accept a list of either one or two numbers and haveBL÷BITLƲ),M
for 11 * 0.7 = 7.7. (Maybe this comparison should be fixed...)
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
...BTW that isn't doing what you thinkIṠ$
is acting on the input list itself so it's just telling you that 10 is greater than 5 rather than comparing the smoothness of the two numbers.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
7 hours ago
add a comment |
$begingroup$
05AB1E, 8 bytes/characters
bgIbγg</
Try it online or verify all test cases.
Or alternatively:
b©g®¥ÄO/
Try it online or verify all test cases.
Outputs 0.0
for the INF
cases.
With both bonuses score: 11.97 (18 bytes/characters * 0.95 * 0.7):
εbgybγg</3.ò}DÆ.±)
Outputs 1
if the first input is larger than the second; -1
if vice-versa; 0
if they are equal.
NOTE: Because I output 0.0
for the INF
cases, they are considered lower than non-infinity test cases. Let me know if this has to be fixed..
Try it online.
Explanation:
ε # Map both values of the (implicit) input-list:
b # Get the binary-string of the current value
g # And get the length of this string
yb # Get the binary-string of the current value again
γ # Split it into chunks of equal adjacent digits
g< # Get the amount of chunks, and subtract 1
/ # Divide both numbers
3.ò # Round the number to 3 decimal values
}D # After the map: duplicate the resulting list
Æ # Reduce the duplicated list by subtraction
.± # And get the sign of that result
) # Then wrap it into a list with the mapped values
# (and output the result implicitly)
$endgroup$
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
8 hours ago
1
$begingroup$
@LuisMendo Yes, but 05AB1E doesn't have an infinite value (unless you count the infinite list). And since regular cases which dividex
byy
can never result in0
anyway, I use that to indicate infinity (the challenge description states "you need to output something that tells you infinity", which is0.0
in my answer). If the infinite list or an empty string or something has to be output instead of 0 it's 3 bytes more, although I don't really see the point..
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
I'll accept this answer as valid as there are no fractions that naturally result in zero. You would need to define something (like5/1 = 0 = Infinity
) to get 0.
$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
8 hours ago
add a comment |
$begingroup$
Japt, 8 bytes
NULL for infinity
¤Ê/¢ä¦ x
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 46 44 40 bytes / characters
Returns Infinity
if there's no bit flip.
n=>(g=s=>n&&1+g(x=s-(n^(n>>=1))%2))``/~x
Try it online!
Commented
n => ( // n = input integer
g = s => // g = recursive function taking the number s of bit switches
n && // stop if n is equal to 0
1 + // otherwise, add 1 to the final returned value
g( // and do a recursive call to g:
x = // update s and save the result in x:
s - // subtract 1 from s if ...
(n ^ (n >>= 1)) // ... there is a bit switch; and shift n to the right
% 2 // NB: an extra bit switch is counted on the last bit
) // end of recursive call
)`` // initial call to g with s = [''], which is coerced to 0
// as soon as something is subtracted from it
/ ~x // divide the result of g by -(x + 1), which compensates for
// the extra switch
$endgroup$
add a comment |
$begingroup$
Kotlin, 83 82 bytes
{s->s.toString(2).run{length.toFloat()/(0..length-2).count{this[it]!=this[it+1]}}}
Try it online!
New contributor
$endgroup$
add a comment |
$begingroup$
1. Python 3, 123 bytes (146 with file header)
Hi. I know that my code is way longer than others, but I will try it ;)
Script takes sequence of numbers in aguments and prints "smoothness" for each argument on own line. If number of changes is 0, prints "inf".
$ ./script.py 12
4.0
$ ./script.py 12 5 6
4.0
1.5
3.0
$ ./script.py `seq 5`
None
2.0
None
3.0
1.5
file header
#!/usr/bin/env python3
code
for n in argv[1:]:
b=bin(int(n))[2:];c=0;l=b[0]
for o in b:
if o!=l:c+=1
l=o
print(len(b)/c if c>0 else"inf")
New contributor
$endgroup$
add a comment |
$begingroup$
R, 56 bytes
length(y<-(x=scan())%/%2^(0:log2(x))%%2)/sum(diff(y)!=0)
Try it online!
$endgroup$
add a comment |
$begingroup$
Charcoal, 24 characters
≔⍘N²θ≔⁺№θ10№θ01η¿ηI∕Lθη∞
Try it online! Link is to verbose version of code. Explanation:
≔⍘N²θ
Input the number and convert it to base 2 as a string.
≔⁺№θ10№θ01η
Calculate the number of of switches by counting the occurrences of 10
or 01
in the string.
¿ηI∕Lθη∞
If the total is nonzero then output the smoothness otherwise print Infinity.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180928%2ffind-how-smooth-a-number-is-based-on-binary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Perl 5 (-p -Mbignum
), 45 bytes
$_=sprintf"%b",$_;$_=y///c/(s/(.)(?!1)//g-1)
TIO
$endgroup$
add a comment |
$begingroup$
Perl 5 (-p -Mbignum
), 45 bytes
$_=sprintf"%b",$_;$_=y///c/(s/(.)(?!1)//g-1)
TIO
$endgroup$
add a comment |
$begingroup$
Perl 5 (-p -Mbignum
), 45 bytes
$_=sprintf"%b",$_;$_=y///c/(s/(.)(?!1)//g-1)
TIO
$endgroup$
Perl 5 (-p -Mbignum
), 45 bytes
$_=sprintf"%b",$_;$_=y///c/(s/(.)(?!1)//g-1)
TIO
answered 8 hours ago
Nahuel FouilleulNahuel Fouilleul
2,69529
2,69529
add a comment |
add a comment |
$begingroup$
MATL, 7 characters
Btnwdz/
Try it online!
Explanation
B % Convert to binary
t % Duplicate
n % Number of elements
w % Swap
d % Consecutive differences
z % Number of nonzeros
/ % Divide
$endgroup$
2
$begingroup$
Honestly, how it it possible to incorporate 7 ASCII characters to make a complicated program like the one I described. I don't understand code golf anymore.
$endgroup$
– Ethan Slota
10 hours ago
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
10 hours ago
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
9 hours ago
1
$begingroup$
@EthanSlota MATL is at least pretty easy to understand, since for the most part, each command is a MATLAB or Octave command and there's a parser built in Octave / MATLAB. This would translate to something like@(x,b=dec2bin(x))numel(b)/nnz(diff(b))
but it uses Luis' much terser language to achieve the same result.
$endgroup$
– Giuseppe
9 hours ago
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
9 hours ago
|
show 2 more comments
$begingroup$
MATL, 7 characters
Btnwdz/
Try it online!
Explanation
B % Convert to binary
t % Duplicate
n % Number of elements
w % Swap
d % Consecutive differences
z % Number of nonzeros
/ % Divide
$endgroup$
2
$begingroup$
Honestly, how it it possible to incorporate 7 ASCII characters to make a complicated program like the one I described. I don't understand code golf anymore.
$endgroup$
– Ethan Slota
10 hours ago
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
10 hours ago
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
9 hours ago
1
$begingroup$
@EthanSlota MATL is at least pretty easy to understand, since for the most part, each command is a MATLAB or Octave command and there's a parser built in Octave / MATLAB. This would translate to something like@(x,b=dec2bin(x))numel(b)/nnz(diff(b))
but it uses Luis' much terser language to achieve the same result.
$endgroup$
– Giuseppe
9 hours ago
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
9 hours ago
|
show 2 more comments
$begingroup$
MATL, 7 characters
Btnwdz/
Try it online!
Explanation
B % Convert to binary
t % Duplicate
n % Number of elements
w % Swap
d % Consecutive differences
z % Number of nonzeros
/ % Divide
$endgroup$
MATL, 7 characters
Btnwdz/
Try it online!
Explanation
B % Convert to binary
t % Duplicate
n % Number of elements
w % Swap
d % Consecutive differences
z % Number of nonzeros
/ % Divide
edited 8 hours ago
answered 10 hours ago
Luis MendoLuis Mendo
74.6k888291
74.6k888291
2
$begingroup$
Honestly, how it it possible to incorporate 7 ASCII characters to make a complicated program like the one I described. I don't understand code golf anymore.
$endgroup$
– Ethan Slota
10 hours ago
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
10 hours ago
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
9 hours ago
1
$begingroup$
@EthanSlota MATL is at least pretty easy to understand, since for the most part, each command is a MATLAB or Octave command and there's a parser built in Octave / MATLAB. This would translate to something like@(x,b=dec2bin(x))numel(b)/nnz(diff(b))
but it uses Luis' much terser language to achieve the same result.
$endgroup$
– Giuseppe
9 hours ago
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
9 hours ago
|
show 2 more comments
2
$begingroup$
Honestly, how it it possible to incorporate 7 ASCII characters to make a complicated program like the one I described. I don't understand code golf anymore.
$endgroup$
– Ethan Slota
10 hours ago
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
10 hours ago
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
9 hours ago
1
$begingroup$
@EthanSlota MATL is at least pretty easy to understand, since for the most part, each command is a MATLAB or Octave command and there's a parser built in Octave / MATLAB. This would translate to something like@(x,b=dec2bin(x))numel(b)/nnz(diff(b))
but it uses Luis' much terser language to achieve the same result.
$endgroup$
– Giuseppe
9 hours ago
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
9 hours ago
2
2
$begingroup$
Honestly, how it it possible to incorporate 7 ASCII characters to make a complicated program like the one I described. I don't understand code golf anymore.
$endgroup$
– Ethan Slota
10 hours ago
$begingroup$
Honestly, how it it possible to incorporate 7 ASCII characters to make a complicated program like the one I described. I don't understand code golf anymore.
$endgroup$
– Ethan Slota
10 hours ago
1
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
10 hours ago
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
10 hours ago
1
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
9 hours ago
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
9 hours ago
1
1
$begingroup$
@EthanSlota MATL is at least pretty easy to understand, since for the most part, each command is a MATLAB or Octave command and there's a parser built in Octave / MATLAB. This would translate to something like
@(x,b=dec2bin(x))numel(b)/nnz(diff(b))
but it uses Luis' much terser language to achieve the same result.$endgroup$
– Giuseppe
9 hours ago
$begingroup$
@EthanSlota MATL is at least pretty easy to understand, since for the most part, each command is a MATLAB or Octave command and there's a parser built in Octave / MATLAB. This would translate to something like
@(x,b=dec2bin(x))numel(b)/nnz(diff(b))
but it uses Luis' much terser language to achieve the same result.$endgroup$
– Giuseppe
9 hours ago
1
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
9 hours ago
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
9 hours ago
|
show 2 more comments
$begingroup$
Jelly, 8 bytes, score 8
Maybe there is a terser way... edit: I don't think there is.
BL÷BITLƲ
Infinity is given as inf
.
Try it online!
...other 8's are possible too, for example BµITL÷@L
or BL÷BnƝSƊ
.
$endgroup$
$begingroup$
Here’s a version to get the bonuses: Try it online! score 16 x 0.95 x 0.7 = 10.64
$endgroup$
– Nick Kennedy
8 hours ago
$begingroup$
Shouldn'tinf
be greater than all other values? If we can identifyinf
as less than all others we could accept a list of either one or two numbers and haveBL÷BITLƲ),M
for 11 * 0.7 = 7.7. (Maybe this comparison should be fixed...)
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
...BTW that isn't doing what you thinkIṠ$
is acting on the input list itself so it's just telling you that 10 is greater than 5 rather than comparing the smoothness of the two numbers.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
7 hours ago
add a comment |
$begingroup$
Jelly, 8 bytes, score 8
Maybe there is a terser way... edit: I don't think there is.
BL÷BITLƲ
Infinity is given as inf
.
Try it online!
...other 8's are possible too, for example BµITL÷@L
or BL÷BnƝSƊ
.
$endgroup$
$begingroup$
Here’s a version to get the bonuses: Try it online! score 16 x 0.95 x 0.7 = 10.64
$endgroup$
– Nick Kennedy
8 hours ago
$begingroup$
Shouldn'tinf
be greater than all other values? If we can identifyinf
as less than all others we could accept a list of either one or two numbers and haveBL÷BITLƲ),M
for 11 * 0.7 = 7.7. (Maybe this comparison should be fixed...)
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
...BTW that isn't doing what you thinkIṠ$
is acting on the input list itself so it's just telling you that 10 is greater than 5 rather than comparing the smoothness of the two numbers.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
7 hours ago
add a comment |
$begingroup$
Jelly, 8 bytes, score 8
Maybe there is a terser way... edit: I don't think there is.
BL÷BITLƲ
Infinity is given as inf
.
Try it online!
...other 8's are possible too, for example BµITL÷@L
or BL÷BnƝSƊ
.
$endgroup$
Jelly, 8 bytes, score 8
Maybe there is a terser way... edit: I don't think there is.
BL÷BITLƲ
Infinity is given as inf
.
Try it online!
...other 8's are possible too, for example BµITL÷@L
or BL÷BnƝSƊ
.
edited 8 hours ago
answered 9 hours ago
Jonathan AllanJonathan Allan
52.4k535170
52.4k535170
$begingroup$
Here’s a version to get the bonuses: Try it online! score 16 x 0.95 x 0.7 = 10.64
$endgroup$
– Nick Kennedy
8 hours ago
$begingroup$
Shouldn'tinf
be greater than all other values? If we can identifyinf
as less than all others we could accept a list of either one or two numbers and haveBL÷BITLƲ),M
for 11 * 0.7 = 7.7. (Maybe this comparison should be fixed...)
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
...BTW that isn't doing what you thinkIṠ$
is acting on the input list itself so it's just telling you that 10 is greater than 5 rather than comparing the smoothness of the two numbers.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
7 hours ago
add a comment |
$begingroup$
Here’s a version to get the bonuses: Try it online! score 16 x 0.95 x 0.7 = 10.64
$endgroup$
– Nick Kennedy
8 hours ago
$begingroup$
Shouldn'tinf
be greater than all other values? If we can identifyinf
as less than all others we could accept a list of either one or two numbers and haveBL÷BITLƲ),M
for 11 * 0.7 = 7.7. (Maybe this comparison should be fixed...)
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
...BTW that isn't doing what you thinkIṠ$
is acting on the input list itself so it's just telling you that 10 is greater than 5 rather than comparing the smoothness of the two numbers.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
7 hours ago
$begingroup$
Here’s a version to get the bonuses: Try it online! score 16 x 0.95 x 0.7 = 10.64
$endgroup$
– Nick Kennedy
8 hours ago
$begingroup$
Here’s a version to get the bonuses: Try it online! score 16 x 0.95 x 0.7 = 10.64
$endgroup$
– Nick Kennedy
8 hours ago
$begingroup$
Shouldn't
inf
be greater than all other values? If we can identify inf
as less than all others we could accept a list of either one or two numbers and have BL÷BITLƲ),M
for 11 * 0.7 = 7.7. (Maybe this comparison should be fixed...)$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Shouldn't
inf
be greater than all other values? If we can identify inf
as less than all others we could accept a list of either one or two numbers and have BL÷BITLƲ),M
for 11 * 0.7 = 7.7. (Maybe this comparison should be fixed...)$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
...BTW that isn't doing what you think
IṠ$
is acting on the input list itself so it's just telling you that 10 is greater than 5 rather than comparing the smoothness of the two numbers.$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
...BTW that isn't doing what you think
IṠ$
is acting on the input list itself so it's just telling you that 10 is greater than 5 rather than comparing the smoothness of the two numbers.$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
7 hours ago
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
7 hours ago
add a comment |
$begingroup$
05AB1E, 8 bytes/characters
bgIbγg</
Try it online or verify all test cases.
Or alternatively:
b©g®¥ÄO/
Try it online or verify all test cases.
Outputs 0.0
for the INF
cases.
With both bonuses score: 11.97 (18 bytes/characters * 0.95 * 0.7):
εbgybγg</3.ò}DÆ.±)
Outputs 1
if the first input is larger than the second; -1
if vice-versa; 0
if they are equal.
NOTE: Because I output 0.0
for the INF
cases, they are considered lower than non-infinity test cases. Let me know if this has to be fixed..
Try it online.
Explanation:
ε # Map both values of the (implicit) input-list:
b # Get the binary-string of the current value
g # And get the length of this string
yb # Get the binary-string of the current value again
γ # Split it into chunks of equal adjacent digits
g< # Get the amount of chunks, and subtract 1
/ # Divide both numbers
3.ò # Round the number to 3 decimal values
}D # After the map: duplicate the resulting list
Æ # Reduce the duplicated list by subtraction
.± # And get the sign of that result
) # Then wrap it into a list with the mapped values
# (and output the result implicitly)
$endgroup$
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
8 hours ago
1
$begingroup$
@LuisMendo Yes, but 05AB1E doesn't have an infinite value (unless you count the infinite list). And since regular cases which dividex
byy
can never result in0
anyway, I use that to indicate infinity (the challenge description states "you need to output something that tells you infinity", which is0.0
in my answer). If the infinite list or an empty string or something has to be output instead of 0 it's 3 bytes more, although I don't really see the point..
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
I'll accept this answer as valid as there are no fractions that naturally result in zero. You would need to define something (like5/1 = 0 = Infinity
) to get 0.
$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
8 hours ago
add a comment |
$begingroup$
05AB1E, 8 bytes/characters
bgIbγg</
Try it online or verify all test cases.
Or alternatively:
b©g®¥ÄO/
Try it online or verify all test cases.
Outputs 0.0
for the INF
cases.
With both bonuses score: 11.97 (18 bytes/characters * 0.95 * 0.7):
εbgybγg</3.ò}DÆ.±)
Outputs 1
if the first input is larger than the second; -1
if vice-versa; 0
if they are equal.
NOTE: Because I output 0.0
for the INF
cases, they are considered lower than non-infinity test cases. Let me know if this has to be fixed..
Try it online.
Explanation:
ε # Map both values of the (implicit) input-list:
b # Get the binary-string of the current value
g # And get the length of this string
yb # Get the binary-string of the current value again
γ # Split it into chunks of equal adjacent digits
g< # Get the amount of chunks, and subtract 1
/ # Divide both numbers
3.ò # Round the number to 3 decimal values
}D # After the map: duplicate the resulting list
Æ # Reduce the duplicated list by subtraction
.± # And get the sign of that result
) # Then wrap it into a list with the mapped values
# (and output the result implicitly)
$endgroup$
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
8 hours ago
1
$begingroup$
@LuisMendo Yes, but 05AB1E doesn't have an infinite value (unless you count the infinite list). And since regular cases which dividex
byy
can never result in0
anyway, I use that to indicate infinity (the challenge description states "you need to output something that tells you infinity", which is0.0
in my answer). If the infinite list or an empty string or something has to be output instead of 0 it's 3 bytes more, although I don't really see the point..
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
I'll accept this answer as valid as there are no fractions that naturally result in zero. You would need to define something (like5/1 = 0 = Infinity
) to get 0.
$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
8 hours ago
add a comment |
$begingroup$
05AB1E, 8 bytes/characters
bgIbγg</
Try it online or verify all test cases.
Or alternatively:
b©g®¥ÄO/
Try it online or verify all test cases.
Outputs 0.0
for the INF
cases.
With both bonuses score: 11.97 (18 bytes/characters * 0.95 * 0.7):
εbgybγg</3.ò}DÆ.±)
Outputs 1
if the first input is larger than the second; -1
if vice-versa; 0
if they are equal.
NOTE: Because I output 0.0
for the INF
cases, they are considered lower than non-infinity test cases. Let me know if this has to be fixed..
Try it online.
Explanation:
ε # Map both values of the (implicit) input-list:
b # Get the binary-string of the current value
g # And get the length of this string
yb # Get the binary-string of the current value again
γ # Split it into chunks of equal adjacent digits
g< # Get the amount of chunks, and subtract 1
/ # Divide both numbers
3.ò # Round the number to 3 decimal values
}D # After the map: duplicate the resulting list
Æ # Reduce the duplicated list by subtraction
.± # And get the sign of that result
) # Then wrap it into a list with the mapped values
# (and output the result implicitly)
$endgroup$
05AB1E, 8 bytes/characters
bgIbγg</
Try it online or verify all test cases.
Or alternatively:
b©g®¥ÄO/
Try it online or verify all test cases.
Outputs 0.0
for the INF
cases.
With both bonuses score: 11.97 (18 bytes/characters * 0.95 * 0.7):
εbgybγg</3.ò}DÆ.±)
Outputs 1
if the first input is larger than the second; -1
if vice-versa; 0
if they are equal.
NOTE: Because I output 0.0
for the INF
cases, they are considered lower than non-infinity test cases. Let me know if this has to be fixed..
Try it online.
Explanation:
ε # Map both values of the (implicit) input-list:
b # Get the binary-string of the current value
g # And get the length of this string
yb # Get the binary-string of the current value again
γ # Split it into chunks of equal adjacent digits
g< # Get the amount of chunks, and subtract 1
/ # Divide both numbers
3.ò # Round the number to 3 decimal values
}D # After the map: duplicate the resulting list
Æ # Reduce the duplicated list by subtraction
.± # And get the sign of that result
) # Then wrap it into a list with the mapped values
# (and output the result implicitly)
answered 9 hours ago
Kevin CruijssenKevin Cruijssen
39.5k560203
39.5k560203
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
8 hours ago
1
$begingroup$
@LuisMendo Yes, but 05AB1E doesn't have an infinite value (unless you count the infinite list). And since regular cases which dividex
byy
can never result in0
anyway, I use that to indicate infinity (the challenge description states "you need to output something that tells you infinity", which is0.0
in my answer). If the infinite list or an empty string or something has to be output instead of 0 it's 3 bytes more, although I don't really see the point..
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
I'll accept this answer as valid as there are no fractions that naturally result in zero. You would need to define something (like5/1 = 0 = Infinity
) to get 0.
$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
8 hours ago
add a comment |
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
8 hours ago
1
$begingroup$
@LuisMendo Yes, but 05AB1E doesn't have an infinite value (unless you count the infinite list). And since regular cases which dividex
byy
can never result in0
anyway, I use that to indicate infinity (the challenge description states "you need to output something that tells you infinity", which is0.0
in my answer). If the infinite list or an empty string or something has to be output instead of 0 it's 3 bytes more, although I don't really see the point..
$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
I'll accept this answer as valid as there are no fractions that naturally result in zero. You would need to define something (like5/1 = 0 = Infinity
) to get 0.
$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
8 hours ago
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
8 hours ago
1
1
$begingroup$
@LuisMendo Yes, but 05AB1E doesn't have an infinite value (unless you count the infinite list). And since regular cases which divide
x
by y
can never result in 0
anyway, I use that to indicate infinity (the challenge description states "you need to output something that tells you infinity", which is 0.0
in my answer). If the infinite list or an empty string or something has to be output instead of 0 it's 3 bytes more, although I don't really see the point..$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
@LuisMendo Yes, but 05AB1E doesn't have an infinite value (unless you count the infinite list). And since regular cases which divide
x
by y
can never result in 0
anyway, I use that to indicate infinity (the challenge description states "you need to output something that tells you infinity", which is 0.0
in my answer). If the infinite list or an empty string or something has to be output instead of 0 it's 3 bytes more, although I don't really see the point..$endgroup$
– Kevin Cruijssen
8 hours ago
$begingroup$
I'll accept this answer as valid as there are no fractions that naturally result in zero. You would need to define something (like
5/1 = 0 = Infinity
) to get 0.$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
I'll accept this answer as valid as there are no fractions that naturally result in zero. You would need to define something (like
5/1 = 0 = Infinity
) to get 0.$endgroup$
– Ethan Slota
8 hours ago
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
8 hours ago
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
8 hours ago
add a comment |
$begingroup$
Japt, 8 bytes
NULL for infinity
¤Ê/¢ä¦ x
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt, 8 bytes
NULL for infinity
¤Ê/¢ä¦ x
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt, 8 bytes
NULL for infinity
¤Ê/¢ä¦ x
Try it online!
$endgroup$
Japt, 8 bytes
NULL for infinity
¤Ê/¢ä¦ x
Try it online!
answered 9 hours ago
Luis felipe De jesus MunozLuis felipe De jesus Munoz
5,48821670
5,48821670
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 46 44 40 bytes / characters
Returns Infinity
if there's no bit flip.
n=>(g=s=>n&&1+g(x=s-(n^(n>>=1))%2))``/~x
Try it online!
Commented
n => ( // n = input integer
g = s => // g = recursive function taking the number s of bit switches
n && // stop if n is equal to 0
1 + // otherwise, add 1 to the final returned value
g( // and do a recursive call to g:
x = // update s and save the result in x:
s - // subtract 1 from s if ...
(n ^ (n >>= 1)) // ... there is a bit switch; and shift n to the right
% 2 // NB: an extra bit switch is counted on the last bit
) // end of recursive call
)`` // initial call to g with s = [''], which is coerced to 0
// as soon as something is subtracted from it
/ ~x // divide the result of g by -(x + 1), which compensates for
// the extra switch
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 46 44 40 bytes / characters
Returns Infinity
if there's no bit flip.
n=>(g=s=>n&&1+g(x=s-(n^(n>>=1))%2))``/~x
Try it online!
Commented
n => ( // n = input integer
g = s => // g = recursive function taking the number s of bit switches
n && // stop if n is equal to 0
1 + // otherwise, add 1 to the final returned value
g( // and do a recursive call to g:
x = // update s and save the result in x:
s - // subtract 1 from s if ...
(n ^ (n >>= 1)) // ... there is a bit switch; and shift n to the right
% 2 // NB: an extra bit switch is counted on the last bit
) // end of recursive call
)`` // initial call to g with s = [''], which is coerced to 0
// as soon as something is subtracted from it
/ ~x // divide the result of g by -(x + 1), which compensates for
// the extra switch
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 46 44 40 bytes / characters
Returns Infinity
if there's no bit flip.
n=>(g=s=>n&&1+g(x=s-(n^(n>>=1))%2))``/~x
Try it online!
Commented
n => ( // n = input integer
g = s => // g = recursive function taking the number s of bit switches
n && // stop if n is equal to 0
1 + // otherwise, add 1 to the final returned value
g( // and do a recursive call to g:
x = // update s and save the result in x:
s - // subtract 1 from s if ...
(n ^ (n >>= 1)) // ... there is a bit switch; and shift n to the right
% 2 // NB: an extra bit switch is counted on the last bit
) // end of recursive call
)`` // initial call to g with s = [''], which is coerced to 0
// as soon as something is subtracted from it
/ ~x // divide the result of g by -(x + 1), which compensates for
// the extra switch
$endgroup$
JavaScript (ES6), 46 44 40 bytes / characters
Returns Infinity
if there's no bit flip.
n=>(g=s=>n&&1+g(x=s-(n^(n>>=1))%2))``/~x
Try it online!
Commented
n => ( // n = input integer
g = s => // g = recursive function taking the number s of bit switches
n && // stop if n is equal to 0
1 + // otherwise, add 1 to the final returned value
g( // and do a recursive call to g:
x = // update s and save the result in x:
s - // subtract 1 from s if ...
(n ^ (n >>= 1)) // ... there is a bit switch; and shift n to the right
% 2 // NB: an extra bit switch is counted on the last bit
) // end of recursive call
)`` // initial call to g with s = [''], which is coerced to 0
// as soon as something is subtracted from it
/ ~x // divide the result of g by -(x + 1), which compensates for
// the extra switch
edited 6 hours ago
answered 10 hours ago
ArnauldArnauld
77.8k694325
77.8k694325
add a comment |
add a comment |
$begingroup$
Kotlin, 83 82 bytes
{s->s.toString(2).run{length.toFloat()/(0..length-2).count{this[it]!=this[it+1]}}}
Try it online!
New contributor
$endgroup$
add a comment |
$begingroup$
Kotlin, 83 82 bytes
{s->s.toString(2).run{length.toFloat()/(0..length-2).count{this[it]!=this[it+1]}}}
Try it online!
New contributor
$endgroup$
add a comment |
$begingroup$
Kotlin, 83 82 bytes
{s->s.toString(2).run{length.toFloat()/(0..length-2).count{this[it]!=this[it+1]}}}
Try it online!
New contributor
$endgroup$
Kotlin, 83 82 bytes
{s->s.toString(2).run{length.toFloat()/(0..length-2).count{this[it]!=this[it+1]}}}
Try it online!
New contributor
edited 9 hours ago
New contributor
answered 9 hours ago
AdamAdam
11
11
New contributor
New contributor
add a comment |
add a comment |
$begingroup$
1. Python 3, 123 bytes (146 with file header)
Hi. I know that my code is way longer than others, but I will try it ;)
Script takes sequence of numbers in aguments and prints "smoothness" for each argument on own line. If number of changes is 0, prints "inf".
$ ./script.py 12
4.0
$ ./script.py 12 5 6
4.0
1.5
3.0
$ ./script.py `seq 5`
None
2.0
None
3.0
1.5
file header
#!/usr/bin/env python3
code
for n in argv[1:]:
b=bin(int(n))[2:];c=0;l=b[0]
for o in b:
if o!=l:c+=1
l=o
print(len(b)/c if c>0 else"inf")
New contributor
$endgroup$
add a comment |
$begingroup$
1. Python 3, 123 bytes (146 with file header)
Hi. I know that my code is way longer than others, but I will try it ;)
Script takes sequence of numbers in aguments and prints "smoothness" for each argument on own line. If number of changes is 0, prints "inf".
$ ./script.py 12
4.0
$ ./script.py 12 5 6
4.0
1.5
3.0
$ ./script.py `seq 5`
None
2.0
None
3.0
1.5
file header
#!/usr/bin/env python3
code
for n in argv[1:]:
b=bin(int(n))[2:];c=0;l=b[0]
for o in b:
if o!=l:c+=1
l=o
print(len(b)/c if c>0 else"inf")
New contributor
$endgroup$
add a comment |
$begingroup$
1. Python 3, 123 bytes (146 with file header)
Hi. I know that my code is way longer than others, but I will try it ;)
Script takes sequence of numbers in aguments and prints "smoothness" for each argument on own line. If number of changes is 0, prints "inf".
$ ./script.py 12
4.0
$ ./script.py 12 5 6
4.0
1.5
3.0
$ ./script.py `seq 5`
None
2.0
None
3.0
1.5
file header
#!/usr/bin/env python3
code
for n in argv[1:]:
b=bin(int(n))[2:];c=0;l=b[0]
for o in b:
if o!=l:c+=1
l=o
print(len(b)/c if c>0 else"inf")
New contributor
$endgroup$
1. Python 3, 123 bytes (146 with file header)
Hi. I know that my code is way longer than others, but I will try it ;)
Script takes sequence of numbers in aguments and prints "smoothness" for each argument on own line. If number of changes is 0, prints "inf".
$ ./script.py 12
4.0
$ ./script.py 12 5 6
4.0
1.5
3.0
$ ./script.py `seq 5`
None
2.0
None
3.0
1.5
file header
#!/usr/bin/env python3
code
for n in argv[1:]:
b=bin(int(n))[2:];c=0;l=b[0]
for o in b:
if o!=l:c+=1
l=o
print(len(b)/c if c>0 else"inf")
New contributor
New contributor
answered 6 hours ago
ReneRene
11
11
New contributor
New contributor
add a comment |
add a comment |
$begingroup$
R, 56 bytes
length(y<-(x=scan())%/%2^(0:log2(x))%%2)/sum(diff(y)!=0)
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 56 bytes
length(y<-(x=scan())%/%2^(0:log2(x))%%2)/sum(diff(y)!=0)
Try it online!
$endgroup$
add a comment |
$begingroup$
R, 56 bytes
length(y<-(x=scan())%/%2^(0:log2(x))%%2)/sum(diff(y)!=0)
Try it online!
$endgroup$
R, 56 bytes
length(y<-(x=scan())%/%2^(0:log2(x))%%2)/sum(diff(y)!=0)
Try it online!
answered 4 hours ago
Nick KennedyNick Kennedy
40125
40125
add a comment |
add a comment |
$begingroup$
Charcoal, 24 characters
≔⍘N²θ≔⁺№θ10№θ01η¿ηI∕Lθη∞
Try it online! Link is to verbose version of code. Explanation:
≔⍘N²θ
Input the number and convert it to base 2 as a string.
≔⁺№θ10№θ01η
Calculate the number of of switches by counting the occurrences of 10
or 01
in the string.
¿ηI∕Lθη∞
If the total is nonzero then output the smoothness otherwise print Infinity.
$endgroup$
add a comment |
$begingroup$
Charcoal, 24 characters
≔⍘N²θ≔⁺№θ10№θ01η¿ηI∕Lθη∞
Try it online! Link is to verbose version of code. Explanation:
≔⍘N²θ
Input the number and convert it to base 2 as a string.
≔⁺№θ10№θ01η
Calculate the number of of switches by counting the occurrences of 10
or 01
in the string.
¿ηI∕Lθη∞
If the total is nonzero then output the smoothness otherwise print Infinity.
$endgroup$
add a comment |
$begingroup$
Charcoal, 24 characters
≔⍘N²θ≔⁺№θ10№θ01η¿ηI∕Lθη∞
Try it online! Link is to verbose version of code. Explanation:
≔⍘N²θ
Input the number and convert it to base 2 as a string.
≔⁺№θ10№θ01η
Calculate the number of of switches by counting the occurrences of 10
or 01
in the string.
¿ηI∕Lθη∞
If the total is nonzero then output the smoothness otherwise print Infinity.
$endgroup$
Charcoal, 24 characters
≔⍘N²θ≔⁺№θ10№θ01η¿ηI∕Lθη∞
Try it online! Link is to verbose version of code. Explanation:
≔⍘N²θ
Input the number and convert it to base 2 as a string.
≔⁺№θ10№θ01η
Calculate the number of of switches by counting the occurrences of 10
or 01
in the string.
¿ηI∕Lθη∞
If the total is nonzero then output the smoothness otherwise print Infinity.
answered 3 hours ago
NeilNeil
81.3k745178
81.3k745178
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180928%2ffind-how-smooth-a-number-is-based-on-binary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
$begingroup$
It would be a bit clearer if both examples were given separately. Also, I don't think the bonuses add much to the challenge.
$endgroup$
– Arnauld
10 hours ago
3
$begingroup$
welcome to PPCG! You've been around for a month or so, but I would suggest using The Sandbox to get feedback on your challenges before posting them.
$endgroup$
– Giuseppe
10 hours ago
2
$begingroup$
Inputs
1
,3
,7
etc give infinity. Should we really output infinity for those?$endgroup$
– Luis Mendo
10 hours ago
7
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
9 hours ago
6
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
9 hours ago