r/dailyprogrammer • u/jnazario 2 0 • Dec 11 '17
[2017-12-11] Challenge #344 [Easy] Baum-Sweet Sequence
Description
In mathematics, the Baum–Sweet sequence is an infinite automatic sequence of 0s and 1s defined by the rule:
- b_n = 1 if the binary representation of n contains no block of consecutive 0s of odd length;
- b_n = 0 otherwise;
for n >= 0.
For example, b_4 = 1 because the binary representation of 4 is 100, which only contains one block of consecutive 0s of length 2; whereas b_5 = 0 because the binary representation of 5 is 101, which contains a block of consecutive 0s of length 1. When n is 19611206, b_n is 0 because:
19611206 = 1001010110011111001000110 base 2
00 0 0 00 00 000 0 runs of 0s
^ ^ ^^^ odd length sequencesBecause we find an odd length sequence of 0s, b_n is 0.
Challenge Description
Your challenge today is to write a program that generates the Baum-Sweet sequence from 0 to some number n. For example, given "20" your program would emit:
1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0
2
u/jnazario 2 0 Dec 11 '17
Scala Solution
16
u/Cole_from_SE Dec 11 '17
J
Explanation
Because of the way converting to binary works, the most significant bit will always be a 1, which is convenient for us since that means that we'll split the lists the same way each time. This uses the observation that if you split into lists starting with a 1, the lengths of all these lists must be odd for
b_nto be 1, otherwise it's 0.Visual Explanation
I build up
b_nfor an individual number (4632) following the same algorithm used in my answer. This is done on the REPL: inputs are spaced with three spaces and outputs are not.16
u/mn-haskell-guy 1 0 Dec 12 '17 edited Dec 12 '17
Solution in BrainF*ck. Reads the number from stdin. On average takes about 600 steps to compute a number's Baum-Sweet value (for numbers up to 10000.)
1
u/jnazario 2 0 Dec 11 '17
long time DP readers know i love a good integer sequence to play with ...
Go Solution
4
u/skeeto -9 8 Dec 11 '17
C, just counting runs of bits.
4
u/thorwing Dec 11 '17
Java 9
Output
4
u/jephthai Dec 11 '17 edited Dec 11 '17
Forth
Comments or improvements welcome!
Running:
4
u/svgwrk Dec 12 '17
Well, I didn't post this yesterday, but I couldn't resist posting it. How could I resist a puzzle named "Bomb Suite"? (My spelling may be off.)
4
u/Anonsicide Dec 16 '17
Python
My solution is porbably MUCH longer than it should be. However, I wanted to take the time to write my own function for converting integer to binary, just for the fun of it (I'm sure python has some wizardlike one-liner to do this). Also, my implementation for baumSweet is... wanting. It works, but it's pretty hard to understand, what with all the index manipulation going on.
This is the first of these I've participated in -- looking forward to doing more! :)
3
u/yeah_i_got_skills Dec 11 '17
Powershell:
3
u/Specter_Terrasbane Dec 11 '17
Python 2
2
u/[deleted] Dec 11 '17 edited Dec 13 '17
C
Feedback welcome. Wanted to solve this in a different way from anyone who has posted so far. According to this, the position of 1's are given by the sequence defined here.
Code:
2
u/thestoicattack Dec 11 '17 edited Dec 12 '17
Ada
2
u/drewfer Dec 11 '17 edited Dec 11 '17
Rust
2
u/Accelon2 Dec 11 '17
Python3
Feedback appreciated.
Question: Shouldn't b_0 return 0 in OP's example? Or am I missing something?
2
u/spoonsnoop Dec 11 '17
in noob Java, constructive criticisms welcome
2
u/chunes 1 2 Dec 12 '17
Factor (The program expects a command line argument as input.)
2
u/mn-haskell-guy 1 0 Dec 12 '17 edited Dec 12 '17
A nice "Project Euler" like extension to this problem is:
Additionally, the sequence s(2k -1), k = 0, 1, ... is interesting in that it will yield a familiar number sequence.
2
u/RobinsonCruiseOh Dec 15 '17
So for the
etc
This does not match with the submitted expected output. Does a consecutive 0 sequence include a single 0? a single digit isn't usually considered a sequence, right?
2
u/NuclearBanane Dec 18 '17 edited Dec 19 '17
Java 8
**Edit: Missed the part where its print the range woops.... fixed now
2
u/sitefall Dec 22 '17
First time trying this out. I'm happy to hear your criticisms of my horrible mangled code here. There's a link to the working app here. Code is below:
JS
CSS
HTML
2
u/Wilfred-kun Dec 29 '17 edited Jan 15 '18
Python 3
insert "Back up, I got Regular Expression" comic hereEdit: u/c0r3__ pointed out
bin(x[2:])should bebin(x)[2:]2
u/darknes1234 Jan 02 '18
C++
1
u/Gprime5 Dec 11 '17 edited Dec 12 '17
Python 3.5
1
u/popillol Dec 11 '17
Go / Golang Playground Link. Since /u/jnazario took the string approach it gave me an excuse to try out the bits package. Hoping it runs faster than the string approach.
1
u/Scara95 Dec 11 '17
Q
example usage
1
u/cheers- Dec 11 '17
Javascript
a memoized implementation: codepen demo
1
u/Rixium Dec 11 '17
JavaScript
There's definately a better way to do this..
1
u/Purce Dec 11 '17
Haskell, new to the language, feedback is welcome.
1
u/thestoicattack Dec 11 '17 edited Dec 12 '17
Haskell
1
u/octolanceae Dec 12 '17
Python3.6
1
u/tokanizar Dec 12 '17
PHP because savage
1
u/ASpueW Dec 12 '17
Rust playground
1
u/g00glen00b Dec 12 '17
JavaScript (ES6):
No clue if this actually works, but by removing
/00/gI should be able to get rid of all even sequences of zeroes, so if it contains a 0 afterwards, it should be odd I guess. I'm still a bit confused about why 0 shouldn't result into 0 though, considering that it's an odd number of zeroes?1
u/trollblut Dec 12 '17 edited Dec 12 '17
C#
€: After optimisation (check for 101 sequence, might be faulty for i >= 262):
1
u/mochancrimthann Dec 12 '17 edited Jan 16 '18
Node/Javascript
1
u/TSLRed Dec 12 '17
Python
TIL you can have else clauses on loops in Python which only execute if you don't break out of the loop.
1
u/Working-M4n Dec 12 '17
Python First python script, neat!
1
u/coder_04 Dec 12 '17
My implementation of this, thoughts? I am a newbie at c++ but thought I'd give it a shot.
1
u/Vanheon Dec 12 '17 edited Jun 11 '18
Really bad C++ code.
1
u/jfinn463 Dec 12 '17
My first crack at Python code, constructive criticism is appreciated. I'm sure this is not the most efficient way to do it, but this is what I came up with!
1
u/Smicks91 Dec 12 '17 edited Dec 12 '17
C++
Similar to some of the ones that I've seen, but I didn't see too many C++ solutions, so I posted mine.
1
u/BaryonicBeing Dec 12 '17 edited Dec 12 '17
Python3 Sketchy try with python, trying to explain it via comments
1
u/ramendik Dec 13 '17
Python3 (but I think it should run on Python2, with somewhat weird print output because of brackets). Uses string processing, not maths, to find the sequences of zeros and check their length. This makes the code simpler (and possibly slower).
1
u/[deleted] Dec 13 '17 edited Dec 13 '17
Kotlin
Edit: I decided to create a slightly modified sequence/generator version as well, just to try something new.
1
u/ghost20000 Dec 13 '17 edited Dec 13 '17
Python3
If you have any suggestions please tell me!
Used 2 functions for no reason...
Edit 1: Thanks /u/mn-haskell-guy
Edit 2: Made it even shorter, planning to shorten it even more.
Edit 3: I'm a very bad person for making my code quite unreadable, but hey, it's only 9 lines! I could probably make it even shorter but I'm going to do older challenges...
Edit 4: Thanks again /u/mn-haskell-guy
1
u/zookeeper_zeke Dec 13 '17
My solution is in C:
1
u/Minolwa Dec 13 '17
Kotlin
1
u/SurplusSix Dec 14 '17
Racket
My first go at doing one of these, learning Racket so feedback very welcome.
1
u/kalender_mesrep Dec 14 '17 edited Dec 14 '17
Python 3 This is my 1st solution without using bitwise operations. Feedback appreciated for my 1st submission.
1
u/Espequair Dec 16 '17
Python 2
1
u/fabiomsm Dec 16 '17
Python3
1
u/fishvilla Dec 17 '17
Javascript
My first submission with PG, and I'm a programming newbie, so I'd love any constructive feedback.
1
u/[deleted] Dec 17 '17
Powershell:
Input:
Output:
Input:
Output:
1
u/marucOG Dec 17 '17
Python
1
u/[deleted] Dec 18 '17
C++
1
u/Wadu436 Dec 18 '17
C#
https://gist.github.com/anonymous/8ea4da67de568db61f3dfd93877b35c5
Feedback welcome :)
1
u/originalrhetoric Dec 18 '17
Python 3
Figured I would do it in two lines for fun.
1
u/Williamboyles Dec 18 '17
Python 3.6
I would greatly appreciate any tips and/or feedback
1
u/DisadvantageousPsyco Dec 19 '17
Working solution in C# https://github.com/csrina/dp_344_BaumSweet Feedback and comments are welcome :D
1
u/adisri Dec 19 '17
Java 8
Not pure Java 8 though since there are stream methods I'm not proficient with that another poster below me has used.
1
u/msvaljek Dec 19 '17
scala
1
u/[deleted] Dec 20 '17
Python3
1
u/ivankahl Dec 20 '17 edited Dec 20 '17
Did this with a one-liner function in C#
1
u/[deleted] Dec 20 '17
Pyhton absolute python beginner
1
u/VladmeK Dec 21 '17 edited Dec 21 '17
Elixir
Very new to both Elixir and functional programming in general, so bear with me.
1
u/grinde Dec 21 '17
JavaScript
1
u/Crawford_Fish Dec 22 '17
Haskell, here goes nothin
1
u/King-Tuts Dec 23 '17 edited Dec 23 '17
Java
Going for readability. The odd part is that the first value in the challenge key appears to be wrong, see Output.
Output:
Code:
1
u/buddycool Dec 23 '17
JAVASCRIPT Just unable to print output of 0 as 1
1
u/primaryobjects Dec 24 '17
R
Gist
Output
1
u/KeenWolfPaw Dec 24 '17
C
1
u/[deleted] Dec 24 '17
Javascript
Output
1
u/[deleted] Dec 25 '17
Solution in Haskell, improvements/comments are welcome!
1
u/SimplyJpk Dec 26 '17 edited Dec 26 '17
Solution in C++
Studying programming, been meaning to find a way to practice outside of class, most of whats taught is C#, haven't touched C++ in over a year now, so hopefully this isn't too crude.Hopefully I'll continue doing these challenges haha. Tips and pointers are certainly welcome.
edit: I was converting the number to binary then working on the figure, I realized I could work on the figure while converting it to avoid additional checks.
1
u/matthew_leon Dec 26 '17
Haskell
"Memoized" recursion using a lazy array.
1
u/zatoichi49 Dec 26 '17 edited Dec 27 '17
Method:
Moving through the range, split the binary representation of each integer on every instance of '1', creating a list of all zero groups. Loop through each list, breaking the loop and returning '0' if it contains any groups of odd length. Return '1' if no odd length groups are found.
Python 3:
Output:
1
u/[deleted] Dec 26 '17
Ruby
Using memoization and this algorithm:
We can keep track of a b_n == 0 with one of two tags:
b_n == 1 is simpler:
b_0 == 1, so we can construct the full sequence purely in respect to previous members:
I was concerned that the raw bit checks could possibly be faster than hash lookup, so I compared against an algorithm that simply counts bits, returning 0 if it hits a 1 after an uneven run of 0s:
The benchmarking shows that memoization pretty quickly wins over:
A little experimentation shows that naive counting is better for anything below about 300 iterations, and then memoization quickly pays off.
1
u/Gylergin Dec 27 '17
Since there hasn't been a new challenge in a while, I decided to go back and do this one.
TI-Basic - Written on my TI-84+. The program first converts the current number in the sequence to a binary list, then it counts the number of consecutive zeros. Because it outputs the sequence as a list, you can only find the first 999 numbers in the BS sequence.
Input:
20
Output:
Notes:
1→L₂(1)at the beginning because it wasn't until after I finished that I noticed that the BS sequence starts at zero, and adding this line was easier than messing with the X-Forloop.fPart(log(X)/log(2))=1is included when determining the largestPower due to rounding errors. For instance,log(16)/log(2)is calculated to be 3.9999999999999 but is displayed as4, soiPart(log(16)/log(2))would return3instead of the desired4.If 2fPart(C/2)=1is for catching numbers that end in an odd sequence of zeros (like 2)If X+1≠dim(L₂)determines if a zero has been added to the list, i.e. the number fails. If a zero wasn't added, the number passes and a one is added.1
u/Mrashes Dec 27 '17
I feel like I messed up the execution as it now just tells you if b_n has the right value but I wanted to share regardless. I'd love feedback! I tried to use this as practice for test running so I broke it up quite a bit. This was my first attempt at one of these challenges after trolling for a while.
EDIT: This was in JAVASCRIPT
1
u/NatePfrom93 Dec 28 '17 edited Dec 28 '17
ruby code golf treatment
1
u/Tavric Dec 28 '17
Python 3, FTP hopefully I'm doing this right.
1
u/limeeattack Dec 28 '17
C++
Since i'm new to programming and especially C++, I'm quite sure this is terribly inefficient.
1
u/juanchi35 Dec 28 '17
Javascript
1
u/Vaglame Dec 29 '17
Haskell, its powerful function management turns out to be pretty useful
1
u/BlasphemousJoshua Dec 29 '17
Swift 4
print(baumSweetSequence(through: 20)) Output:
1
u/unknown_guest17 Dec 29 '17
My Solution using Python3, Regex
1
u/dojoblue Dec 30 '17
Python3:
1
u/ElSeaCL Dec 30 '17 edited Dec 30 '17
Java
Hi, first time posting a solution. Hope I can post the solution correctly.
1
u/[deleted] Dec 31 '17 edited Jan 01 '18
Java
I'm definitely a novice. I'm still really unsure about how to properly call the methods to make the program look coherent. Hopefully practicing and seeing more example will help me out
}
1
u/mrsandtron Jan 01 '18
JavaScript
1
u/Putin_Be_Pootin Jan 01 '18
python 3
This is my first submission and any feedback would be great!
1
u/ExcursionSavvy Jan 01 '18
Python
This is my first submission to this community and I hope to start posting more regularly for practice and good habit.
I would absolutely appreciate any comments and support to improve my coding practice. I'm a Mechanical Engineer that uses coding for hobby projects and robotics, and I'm looking to improve my efficiency and overall skill with Python.
1
u/RageML Jan 04 '18
Python 3
Feedback welcomed.
1
u/ivashkul Jan 07 '18
I'm fairly new to programming, and my code is probably pretty bad, yet I would appreciate any feedback. I decided to find the binary representation of each number by myself. There is a way to make the program much faster by replacing my "lock" with a termination of the loop, but I did not know how to do this. In my program I simply move down a each number in binary and increment a counter each 0 I encounter in a row. Then once I encounter a 1 or the program terminates, I check if the counter is odd or even.
1
u/endermajo Jan 07 '18
JavaScript:
1
u/mochancrimthann Jan 08 '18
Elixir
Still new to the language trying to figure out what's idiomatic and what's not. Feedback is welcome!
1
u/jjrobinson-github Jan 08 '18
I know there is a big backlog that could be used, but are we running out of new ideas, or is this just holiday break time off? Been 1 month since last easy problem.
1
u/pi31415926535897 Jan 14 '18 edited Jan 14 '18
Python
Another absolute beginner here. My first submission, so please let me know if I broke any rule.
1
u/callahman Jan 14 '18
PYTHON 3
First submission! Looks like the other Python3 people have done about the same, but would love criticism.
(Also not sure if hard coding the result for 0 was the right approach)
1
u/SlightlyCyborg Jan 14 '18 edited Jan 14 '18
Common Lisp using pure functions & recursion
1
u/[deleted] Jan 15 '18
Rust
1
u/c0r3__ Jan 15 '18 edited Jan 15 '18
Python
My first submission. Feedback welcomed.
1
u/__dict__ Jan 15 '18
First program in Nim.
1
u/do_hickey Jan 26 '18
Python 3.6
As per usual with my solutions, likely not optimal but as of now it works. Several weeks late so I'm sure no one will be looking, but if anyone is indeed reading, comments are always welcome.
Source:
Output:
1
u/heelo_me Feb 06 '18
Python 3.5, cheeky split() edition
1
u/Tetsumi- 1 0 Mar 31 '18
Racket
1
u/ardx_zero Apr 10 '18
C#
where n is how many numbers to generate
How not to do it
EDIT: forgot to replace a magic numer
1
u/Wizard-of-Koz Apr 11 '18
C# Criticism welcome (No challenge output because I don't understand what exactly is specified. If someone could explain, please?)
0
u/[deleted] Dec 11 '17
[deleted] — view deleted comment
0
u/[deleted] Dec 12 '17
[deleted] — view deleted comment
0
u/[deleted] Dec 13 '17
[deleted] — view deleted comment