Find how “smooth” a number is based on binaryImplementing Binary ArithmeticFind the Smoothest NumberFind...
Lock enemy's y-axis when using Vector3.MoveTowards to follow the player
How do we objectively assess if a dialogue sounds unnatural or cringy?
When was drinking water recognized as crucial in marathon running?
PTIJ: What dummy is the Gemara referring to?
How to disable or uninstall iTunes under High Sierra without disabling SIP
Has Wakanda ever accepted refugees?
Misplaced tyre lever - alternatives?
Can we carry rice to Japan?
Where is the fallacy here?
Reason why dimensional travelling would be restricted
I can't die. Who am I?
I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?
“I had a flat in the centre of town, but I didn’t like living there, so …”
Quitting employee has privileged access to critical information
A bug in Excel? Conditional formatting for marking duplicates also highlights unique value
What can I do if someone tampers with my SSH public key?
Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?
Convergence to a fixed point
Meaning of word ягоза
Practical reasons to have both a large police force and bounty hunting network?
GPL code private and stolen
Make me a metasequence
Difference between 'stomach' and 'uterus'
Would the melodic leap of the opening phrase of Mozart's K545 be considered dissonant?
Find how “smooth” a number is based on binary
Implementing Binary ArithmeticFind the Smoothest NumberFind the simplest value between two values■ or ■+■ or ■+■+■ or ■+■+■+■Four is the magic numberWhen will I have a binary car?Disconnect 4 bitsCount the number of Hamming distance sequencesA Simple PattternThe Binary Binary Expansion
$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
yesterday
4
$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
yesterday
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
yesterday
8
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
yesterday
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
yesterday
|
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 yesterday
Ethan Slota
asked yesterday
Ethan SlotaEthan Slota
243
243
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
yesterday
4
$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
yesterday
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
yesterday
8
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
yesterday
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
yesterday
|
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
yesterday
4
$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
yesterday
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
yesterday
8
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
yesterday
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
yesterday
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
yesterday
$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
yesterday
4
4
$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
yesterday
$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
yesterday
8
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
yesterday
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
yesterday
8
8
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
yesterday
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
yesterday
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
yesterday
$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
yesterday
|
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
yesterday
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
yesterday
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
yesterday
|
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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
yesterday
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
yesterday
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
yesterday
$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
yesterday
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
yesterday
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$
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
21 hours ago
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
2 hours ago
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")
New contributor
$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 yesterday
Nahuel FouilleulNahuel Fouilleul
2,715210
2,715210
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
yesterday
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
yesterday
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
yesterday
|
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
yesterday
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
yesterday
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
yesterday
|
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 yesterday
answered yesterday
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
yesterday
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
yesterday
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
yesterday
|
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
yesterday
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
yesterday
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
yesterday
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
yesterday
$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
yesterday
1
1
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
yesterday
$begingroup$
@Ethan Wait for the 4-byte Jelly or 05AB1E answers...
$endgroup$
– Luis Mendo
yesterday
1
1
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
yesterday
$begingroup$
Note that in my original post I said you would be counted by chars, not bytes. Still stands.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
$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
yesterday
1
1
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
yesterday
$begingroup$
@EthanSlota: Counting by chars instead of bytes can only make the scores even lower.
$endgroup$
– recursive
yesterday
|
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 yesterday
Luis felipe De jesus MunozLuis felipe De jesus Munoz
5,57821670
5,57821670
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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
yesterday
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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
yesterday
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 yesterday
answered yesterday
Jonathan AllanJonathan Allan
52.5k535170
52.5k535170
$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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
yesterday
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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
yesterday
$begingroup$
Thanks - still getting my head around Jelly. This seems better: tio but is two bytes longer
$endgroup$
– Nick Kennedy
yesterday
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
yesterday
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
yesterday
$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
yesterday
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
yesterday
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
yesterday
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
yesterday
$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
yesterday
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
yesterday
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 yesterday
Kevin CruijssenKevin Cruijssen
39.7k560203
39.7k560203
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
$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
yesterday
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
yesterday
add a comment |
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
$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
yesterday
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
yesterday
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
yesterday
$begingroup$
For clarification, ∞ ≠ 0.
$endgroup$
– Ethan Slota
yesterday
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
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
yesterday
$begingroup$
@KevinCruijssen Ah, I see. Thanks for clarifying
$endgroup$
– Luis Mendo
yesterday
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 yesterday
answered yesterday
ArnauldArnauld
77.9k695326
77.9k695326
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$
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
21 hours ago
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
2 hours ago
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$
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
21 hours ago
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
2 hours ago
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 yesterday
New contributor
answered yesterday
AdamAdam
213
213
New contributor
New contributor
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
21 hours ago
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
2 hours ago
add a comment |
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
21 hours ago
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
2 hours ago
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
21 hours ago
$begingroup$
Welcome to PPCG! :)
$endgroup$
– Shaggy
21 hours ago
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
2 hours ago
$begingroup$
thanks, Shaggy!
$endgroup$
– Adam
2 hours ago
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 yesterday
Nick KennedyNick Kennedy
44125
44125
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 yesterday
NeilNeil
81.4k745178
81.4k745178
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")
New contributor
$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")
New contributor
$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")
New contributor
$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")
New contributor
edited 21 hours ago
New contributor
answered yesterday
ReneRene
313
313
New contributor
New contributor
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 9 hours ago
answered 15 hours ago
G BG B
7,8761329
7,8761329
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 15 hours ago
answered 15 hours ago
Henry THenry T
1116
1116
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 14 hours ago
answered 14 hours ago
ovsovs
19.2k21160
19.2k21160
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
yesterday
4
$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
yesterday
8
$begingroup$
What is the reason for the complexity around scoring?
$endgroup$
– Jonathan Allan
yesterday
8
$begingroup$
Hi, I downvoted this challenge due to the complexity around the scoring and the ambiguous-ness related to it.
$endgroup$
– AdmBorkBork
yesterday
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
yesterday