Here is my Rust solution! I think my answer is pretty good except I should be using a better data structure than a Vec here, since I really want to push/pop from both ends. If I did that I would be able to avoid needing to sort and reverse my vec, which would help on the processing speed!
fntwice_linear(size:usize)->Vec<u32>{letmutprocessed:Vec<u32>=vec![];letmutunprocessed:Vec<u32>=vec![];letmutcurrent=1;whileprocessed.len()<size{processed.push(current);// This block of code is NOT the most effiecient// I should switch to a data store that can push/pop// from both ends effieciently, as to avoid needing// the sort AND the revserseunprocessed.push(current*2+1);unprocessed.push(current*3+1);unprocessed.sort();unprocessed=unprocessed.iter().rev().cloned().collect();current=unprocessed.pop().unwrap();}processed}pubfntwice_linear_at(u:usize)->u32{twice_linear(u+1).pop().unwrap()}#[cfg(test)]modtests{usecrate::*;#[test]fnit_works_for_the_base_cases(){assert_eq!(twice_linear_at(0),1);assert_eq!(twice_linear_at(1),3);assert_eq!(twice_linear_at(2),4);}#[test]fnit_works_for_the_example(){assert_eq!(twice_linear_at(3),7);assert_eq!(twice_linear_at(4),9);assert_eq!(twice_linear_at(5),10);assert_eq!(twice_linear_at(6),13);assert_eq!(twice_linear_at(7),15);assert_eq!(twice_linear_at(8),19);assert_eq!(twice_linear_at(9),21);assert_eq!(twice_linear_at(10),22);assert_eq!(twice_linear_at(11),27);}#[test]fnit_can_also_return_the_vec(){assert_eq!(twice_linear(12),vec![1,3,4,7,9,10,13,15,19,21,22,27]);}}
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
Here's my solution in Perl. I actually think its a bit too slow, even when using state variables to cache the results in subsequent calls. Maybe I'll look into optimizing it later.
For n = 100,000 , on an ok laptop, it runs in roughly 45 seconds
importtimen=100000print"n = %d"%ndefinsert(l,val):index=len(l)-1done=Falsewhilenotdoneandindex>=0:ifl[index]==val:done=Trueelifl[index]<val:l.insert(index+1,val)done=Trueelse:index-=1ifnotdone:l.insert(0,val)list=[1]index=0start=time.time()whileindex<n:x=list[0]y=(2*x)+1z=(3*x)+1insert(list,y)insert(list,z)# Remove unnecessary entries from the list, which otherwise
# grows to unreasonable size for large values of n.
list.remove(x)while(index+len(list))>n:list.pop()index+=1end=time.time()print"result = %d"%list[0]print"elapsed time (seconds) = %3.6f"%(end-start)
Let us know if you like these mathematical challenges and we'll stick with the trend.
I like em, but I think it's fun to have a good mix of different types of challenges!
One suggestion is to provide more examples/test cases. Especially for the mathy ones, where the correct answers might not be super obvious to everyone, having many examples/test cases can definitely make it easier! Just a thought!
This is one where it would have been super helpful to have some answers to compare against. Beyond the initial few digits, I'm just assuming things are correct, which may not be true.
Not too good with maths, hope I understood well. Here is a (very inefficient, so I added a constraint to the max num to give) attempt in bash:
#!/bin/bashdeclare-a numbersarr
numbersarr[0]=1
echo"Array position \"n\""read-r myn
# make sure we are given a number and the number is not too largere="^[0-9]+$"if![[$myn=~ $re]]||[$myn-gt 100 ];then
echo"not a number or number too big">&2;exit 1
fi
arrl=1
while[$arrl-le$myn];do
for i in"${numbersarr[@]}";do
let x=2\*i+1
let y=3\*i+1
numbersarr+=($x$y)done
sorted_unique=($(echo"${numbersarr[@]}" |xargs -n1 | sort-gu | xargs))arrl="${#sorted_unique[@]}"done# declare -p sorted_uniqueecho" >> item in pos ${myn}: "${sorted_unique[$myn]}
JavaScript
I've been running late lately... but here is a live demo on CodePen
This one times out on bigger input because of generating
Object.keys(series)every time inside the loopHere is my Rust solution! I think my answer is pretty good except I should be using a better data structure than a
Vechere, since I really want to push/pop from both ends. If I did that I would be able to avoid needing to sort and reverse my vec, which would help on the processing speed!Take a look at std::collections::VecDeque :)
Thank you! I knew there was one that fit the bill better!
Here's my solution in Perl. I actually think its a bit too slow, even when using
statevariables to cache the results in subsequent calls. Maybe I'll look into optimizing it later.I'll write an explainer for it when I get a few minutes later.
For n = 100,000 , on an ok laptop, it runs in roughly 45 seconds
I like em, but I think it's fun to have a good mix of different types of challenges!
One suggestion is to provide more examples/test cases. Especially for the mathy ones, where the correct answers might not be super obvious to everyone, having many examples/test cases can definitely make it easier! Just a thought!
Not sure I have well understood this one ... Am I correct with this JS function ? 🙈
EDIT: added the line
u = Array.from(new Set(u))to get unique values !Nim
Time:
./main 0.04s user 0.01s system 39% cpu 0.113 totalNot familiar with nim. It looks like result is a list, and that duplicate values are being added to it. Is that not the case?
I think you're right! Was testing with too small of an input-set to see any duplicates. :)
Cheap fix (./main 341.16s user 0.52s system 99% cpu 5:42.40 total):
A little late to the party (3am my time), but oh well! Here's my submission:
Full code w/ some tests: gist.github.com/kerrishotts/029c8f...
This is one where it would have been super helpful to have some answers to compare against. Beyond the initial few digits, I'm just assuming things are correct, which may not be true.
Not too good with maths, hope I understood well. Here is a (very inefficient, so I added a constraint to the max num to give) attempt in bash:
Haskell, featuring tail recursion!
Python
Nice challenge.
This fails for larger values of n. For n = 100, for instance, it returns 463 rather than 447.