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 5 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
Mar 5 at 16:15
5
$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
Mar 5 at 16:16
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
Mar 5 at 16:55
9
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
Mar 5 at 17:10
4
$begingroup$
I would suggest that if you are going to select an accepted answer at least wait 1 week
$endgroup$
– Luis felipe De jesus Munoz
Mar 5 at 18:47
|
show 5 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 Mar 5 at 18:52
Ethan Slota
asked Mar 5 at 16:09
Ethan SlotaEthan Slota
294
294
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
Mar 5 at 16:15
5
$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
Mar 5 at 16:16
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
Mar 5 at 16:55
9
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
Mar 5 at 17:10
4
$begingroup$
I would suggest that if you are going to select an accepted answer at least wait 1 week
$endgroup$
– Luis felipe De jesus Munoz
Mar 5 at 18:47
|
show 5 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
Mar 5 at 16:15
5
$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
Mar 5 at 16:16
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
Mar 5 at 16:55
9
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
Mar 5 at 17:10
4
$begingroup$
I would suggest that if you are going to select an accepted answer at least wait 1 week
$endgroup$
– Luis felipe De jesus Munoz
Mar 5 at 18:47
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
Mar 5 at 16:15
$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
Mar 5 at 16:15
5
5
$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
Mar 5 at 16:16
$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
Mar 5 at 16:16
8
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
Mar 5 at 16:55
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
Mar 5 at 16:55
9
9
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
Mar 5 at 17:10
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
Mar 5 at 17:10
4
4
$begingroup$
I would suggest that if you are going to select an accepted answer at least wait 1 week
$endgroup$
– Luis felipe De jesus Munoz
Mar 5 at 18:47
$begingroup$
I would suggest that if you are going to select an accepted answer at least wait 1 week
$endgroup$
– Luis felipe De jesus Munoz
Mar 5 at 18:47
|
show 5 more comments
13 Answers
13
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
Mar 5 at 16:36
2
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
Mar 5 at 16:38
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
Mar 5 at 16:42
2
$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
Mar 5 at 16:47
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
Mar 5 at 17:06
|
show 2 more comments
$begingroup$
Japt, 8 bytes
NULL for infinity
¤Ê/¢ä¦ x
Try it online!
$endgroup$
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
Mar 5 at 18:32
$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
Mar 5 at 18:50
$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
Mar 5 at 18:59
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
Mar 5 at 19:21
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
Mar 5 at 17:48
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
Mar 5 at 17:51
$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
Mar 5 at 17:55
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
Mar 5 at 18:10
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!
$endgroup$
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
Mar 6 at 8:14
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
Mar 7 at 3:21
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 |
$begingroup$
1. Python 3, 131 bytes (154 with file header)
Hi. I know that my code is way longer than others, but I will try it ;) Indentation by tabs.
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
import sys
for n in sys.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")
Input single number from STDIN: 102+23 bytes (code + file header)
n=input();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")
$endgroup$
add a comment |
$begingroup$
Ruby, 42 bytes
->n{1.0*(w=(n^n/2).digits 2).size/~-w.sum}
Try it online!
How?
First step: bitwise XOR of x and x/2. The result will have a bit set to 1 for every switch in the input number plus 1, and so we just need to get the number of digits in base 2, and their sum. Then add some parentheses, and make it a float.
$endgroup$
add a comment |
$begingroup$
Python 3, 105 chars
def s(n):b=bin(n)[2:];l=len(b);c=sum([0if b[i]==b[i+1]else 1for i in range(l-1)]);return l/c if c>0else-1
Returns -1 in the case of an infinity.
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 60 bytes
Port of G B's Ruby answer.
lambda l:(c:=(b:=bin(l^l>>1)).count('1'))>1and(len(b)-2)/~-c
Try it online!
Python 2, 68 bytes
Returns false if the value is infinite.
k=input()
n=s=0
while k:l=k%2;n+=1.;k/=2;s+=l^k%2
print s>1and n/~-s
Try it online!
$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
13 Answers
13
active
oldest
votes
13 Answers
13
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 Mar 5 at 17:47
Nahuel FouilleulNahuel Fouilleul
2,725210
2,725210
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
Mar 5 at 16:36
2
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
Mar 5 at 16:38
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
Mar 5 at 16:42
2
$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
Mar 5 at 16:47
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
Mar 5 at 17:06
|
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
Mar 5 at 16:36
2
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
Mar 5 at 16:38
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
Mar 5 at 16:42
2
$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
Mar 5 at 16:47
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
Mar 5 at 17:06
|
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 Mar 5 at 18:11
answered Mar 5 at 16:31
Luis MendoLuis Mendo
74.7k888291
74.7k888291
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
Mar 5 at 16:36
2
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
Mar 5 at 16:38
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
Mar 5 at 16:42
2
$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
Mar 5 at 16:47
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
Mar 5 at 17:06
|
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
Mar 5 at 16:36
2
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
Mar 5 at 16:38
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
Mar 5 at 16:42
2
$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
Mar 5 at 16:47
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
Mar 5 at 17:06
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
Mar 5 at 16:36
$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
Mar 5 at 16:36
2
2
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
Mar 5 at 16:38
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
Mar 5 at 16:38
1
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
Mar 5 at 16:42
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
Mar 5 at 16:42
2
2
$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
Mar 5 at 16:47
$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
Mar 5 at 16:47
1
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
Mar 5 at 17:06
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
Mar 5 at 17:06
|
show 2 more comments
$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 Mar 5 at 17:23
Luis felipe De jesus MunozLuis felipe De jesus Munoz
5,63821670
5,63821670
add a comment |
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
Mar 5 at 18:32
$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
Mar 5 at 18:50
$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
Mar 5 at 18:59
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
Mar 5 at 19:21
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
Mar 5 at 18:32
$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
Mar 5 at 18:50
$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
Mar 5 at 18:59
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
Mar 5 at 19:21
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 Mar 5 at 18:26
answered Mar 5 at 17:26
Jonathan AllanJonathan Allan
53k535172
53k535172
$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
Mar 5 at 18:32
$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
Mar 5 at 18:50
$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
Mar 5 at 18:59
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
Mar 5 at 19:21
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
Mar 5 at 18:32
$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
Mar 5 at 18:50
$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
Mar 5 at 18:59
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
Mar 5 at 19:21
$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
Mar 5 at 18:32
$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
Mar 5 at 18:32
$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
Mar 5 at 18:50
$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
Mar 5 at 18:50
$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
Mar 5 at 18:59
$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
Mar 5 at 18:59
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
Mar 5 at 19:21
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
Mar 5 at 19:21
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
Mar 5 at 17:48
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
Mar 5 at 17:51
$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
Mar 5 at 17:55
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
Mar 5 at 18:10
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
Mar 5 at 17:48
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
Mar 5 at 17:51
$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
Mar 5 at 17:55
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
Mar 5 at 18:10
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 Mar 5 at 17:10
Kevin CruijssenKevin Cruijssen
40.4k565210
40.4k565210
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
Mar 5 at 17:48
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
Mar 5 at 17:51
$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
Mar 5 at 17:55
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
Mar 5 at 18:10
add a comment |
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
Mar 5 at 17:48
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
Mar 5 at 17:51
$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
Mar 5 at 17:55
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
Mar 5 at 18:10
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
Mar 5 at 17:48
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
Mar 5 at 17:48
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
Mar 5 at 17:51
$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
Mar 5 at 17:51
$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
Mar 5 at 17:55
$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
Mar 5 at 17:55
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
Mar 5 at 18:10
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
Mar 5 at 18:10
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 Mar 5 at 20:09
answered Mar 5 at 16:31
ArnauldArnauld
78.6k795327
78.6k795327
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!
$endgroup$
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
Mar 6 at 8:14
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
Mar 7 at 3:21
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!
$endgroup$
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
Mar 6 at 8:14
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
Mar 7 at 3:21
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!
$endgroup$
Kotlin, 83 82 bytes
{s->s.toString(2).run{length.toFloat()/(0..length-2).count{this[it]!=this[it+1]}}}
Try it online!
edited Mar 5 at 17:16
answered Mar 5 at 17:11
AdamAdam
814
814
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
Mar 6 at 8:14
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
Mar 7 at 3:21
add a comment |
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
Mar 6 at 8:14
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
Mar 7 at 3:21
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
Mar 6 at 8:14
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
Mar 6 at 8:14
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
Mar 7 at 3:21
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
Mar 7 at 3:21
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 Mar 5 at 22:21
Nick KennedyNick Kennedy
66137
66137
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 Mar 5 at 23:23
NeilNeil
81.6k745178
81.6k745178
add a comment |
add a comment |
$begingroup$
1. Python 3, 131 bytes (154 with file header)
Hi. I know that my code is way longer than others, but I will try it ;) Indentation by tabs.
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
import sys
for n in sys.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")
Input single number from STDIN: 102+23 bytes (code + file header)
n=input();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")
$endgroup$
add a comment |
$begingroup$
1. Python 3, 131 bytes (154 with file header)
Hi. I know that my code is way longer than others, but I will try it ;) Indentation by tabs.
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
import sys
for n in sys.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")
Input single number from STDIN: 102+23 bytes (code + file header)
n=input();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")
$endgroup$
add a comment |
$begingroup$
1. Python 3, 131 bytes (154 with file header)
Hi. I know that my code is way longer than others, but I will try it ;) Indentation by tabs.
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
import sys
for n in sys.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")
Input single number from STDIN: 102+23 bytes (code + file header)
n=input();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")
$endgroup$
1. Python 3, 131 bytes (154 with file header)
Hi. I know that my code is way longer than others, but I will try it ;) Indentation by tabs.
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
import sys
for n in sys.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")
Input single number from STDIN: 102+23 bytes (code + file header)
n=input();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")
edited Mar 6 at 8:10
answered Mar 5 at 19:44
ReneRene
1313
1313
add a comment |
add a comment |
$begingroup$
Ruby, 42 bytes
->n{1.0*(w=(n^n/2).digits 2).size/~-w.sum}
Try it online!
How?
First step: bitwise XOR of x and x/2. The result will have a bit set to 1 for every switch in the input number plus 1, and so we just need to get the number of digits in base 2, and their sum. Then add some parentheses, and make it a float.
$endgroup$
add a comment |
$begingroup$
Ruby, 42 bytes
->n{1.0*(w=(n^n/2).digits 2).size/~-w.sum}
Try it online!
How?
First step: bitwise XOR of x and x/2. The result will have a bit set to 1 for every switch in the input number plus 1, and so we just need to get the number of digits in base 2, and their sum. Then add some parentheses, and make it a float.
$endgroup$
add a comment |
$begingroup$
Ruby, 42 bytes
->n{1.0*(w=(n^n/2).digits 2).size/~-w.sum}
Try it online!
How?
First step: bitwise XOR of x and x/2. The result will have a bit set to 1 for every switch in the input number plus 1, and so we just need to get the number of digits in base 2, and their sum. Then add some parentheses, and make it a float.
$endgroup$
Ruby, 42 bytes
->n{1.0*(w=(n^n/2).digits 2).size/~-w.sum}
Try it online!
How?
First step: bitwise XOR of x and x/2. The result will have a bit set to 1 for every switch in the input number plus 1, and so we just need to get the number of digits in base 2, and their sum. Then add some parentheses, and make it a float.
edited Mar 6 at 20:34
answered Mar 6 at 14:18
G BG B
7,9761429
7,9761429
add a comment |
add a comment |
$begingroup$
Python 3, 105 chars
def s(n):b=bin(n)[2:];l=len(b);c=sum([0if b[i]==b[i+1]else 1for i in range(l-1)]);return l/c if c>0else-1
Returns -1 in the case of an infinity.
$endgroup$
add a comment |
$begingroup$
Python 3, 105 chars
def s(n):b=bin(n)[2:];l=len(b);c=sum([0if b[i]==b[i+1]else 1for i in range(l-1)]);return l/c if c>0else-1
Returns -1 in the case of an infinity.
$endgroup$
add a comment |
$begingroup$
Python 3, 105 chars
def s(n):b=bin(n)[2:];l=len(b);c=sum([0if b[i]==b[i+1]else 1for i in range(l-1)]);return l/c if c>0else-1
Returns -1 in the case of an infinity.
$endgroup$
Python 3, 105 chars
def s(n):b=bin(n)[2:];l=len(b);c=sum([0if b[i]==b[i+1]else 1for i in range(l-1)]);return l/c if c>0else-1
Returns -1 in the case of an infinity.
edited Mar 6 at 14:02
answered Mar 6 at 13:56
Henry THenry T
1416
1416
add a comment |
add a comment |
$begingroup$
Python 3.8 (pre-release), 60 bytes
Port of G B's Ruby answer.
lambda l:(c:=(b:=bin(l^l>>1)).count('1'))>1and(len(b)-2)/~-c
Try it online!
Python 2, 68 bytes
Returns false if the value is infinite.
k=input()
n=s=0
while k:l=k%2;n+=1.;k/=2;s+=l^k%2
print s>1and n/~-s
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 60 bytes
Port of G B's Ruby answer.
lambda l:(c:=(b:=bin(l^l>>1)).count('1'))>1and(len(b)-2)/~-c
Try it online!
Python 2, 68 bytes
Returns false if the value is infinite.
k=input()
n=s=0
while k:l=k%2;n+=1.;k/=2;s+=l^k%2
print s>1and n/~-s
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3.8 (pre-release), 60 bytes
Port of G B's Ruby answer.
lambda l:(c:=(b:=bin(l^l>>1)).count('1'))>1and(len(b)-2)/~-c
Try it online!
Python 2, 68 bytes
Returns false if the value is infinite.
k=input()
n=s=0
while k:l=k%2;n+=1.;k/=2;s+=l^k%2
print s>1and n/~-s
Try it online!
$endgroup$
Python 3.8 (pre-release), 60 bytes
Port of G B's Ruby answer.
lambda l:(c:=(b:=bin(l^l>>1)).count('1'))>1and(len(b)-2)/~-c
Try it online!
Python 2, 68 bytes
Returns false if the value is infinite.
k=input()
n=s=0
while k:l=k%2;n+=1.;k/=2;s+=l^k%2
print s>1and n/~-s
Try it online!
edited Mar 6 at 15:17
answered Mar 6 at 15:02
ovsovs
19.3k21160
19.3k21160
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
Mar 5 at 16:15
5
$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
Mar 5 at 16:16
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
Mar 5 at 16:55
9
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
Mar 5 at 17:10
4
$begingroup$
I would suggest that if you are going to select an accepted answer at least wait 1 week
$endgroup$
– Luis felipe De jesus Munoz
Mar 5 at 18:47