Challenge: The Phantom Bash Loop

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
I mean I can always check for 0 first, just that I lazy type out on my phone haha
Extra newline at the top, and extra space at the back ? I know it works on display. That's the quirk.
Because my question didn't mention where the output is, Technically you can be right.
How about if it must strictly follow the output ? Would you be able to do it ?
:)
 

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
Extra newline at the top, and extra space at the back ? I know it works on display. That's the quirk.
Because my question didn't mention where the output is, Technically you can be right.
How about if it must strictly follow the output ? Would you be able to do it ?
:)
U mean if u add in two new conditions?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
U mean if u add in two new conditions?
I didn't add 2 conditions. My output has no newline at the beginning and strictly speaking if this is a leetcode, an extra space at the back wouldn't pass. What do you think ?
That's why I said technically you can be right.

If I do a diff between your output and mine, it wouldn't be the same, would it ?

But I must admit you are very near one possible solution, and I like this solution.
Lets get this over with so that we can move on to the next one ?
:)
 
Last edited:

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
I didn't add 2 conditions. My output has no newline at the beginning and strictly speaking if this is a leetcode, an extra space at the back wouldn't pass. What do you think ?
That's why I said technically you can be right.

If I do a diff between your output and mine, it wouldn't be the same, would it ?

But I must admit you are very near one possible solution, and I like this solution.
Lets get this over with so that we can move on to the next one ?
:)


end=$1

eval "echo -e {1..$1}| tr ' ' '\n'"
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
end=$1

eval "printf '%d\n' {1..$end}"
Yeah! Finally someone whom knows BASHing.... but still incomplete. That being said, it's trivial compared to what was given so far.

Here I will also provide another variant of what was originally proposed by @diminishin
I must say @diminishin solution is better than my original one (not the one below).
Bash:
[[ $1 > 0 ]] && eval "A=\`echo -e {1..$1}\`" && echo -e "${A// /\\n}"

Let me share the next level up challenge first!
:)
 

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
Yeah! Finally someone whom knows BASHing.... :)

Here I will also provide another variant of what was originally proposed by @diminishin
I must say @diminishin solution is better than my original one (not the one below).
Bash:
[[ $1 > 0 ]] && eval "A=\`echo -e {1..$1}\`" && echo -e "${A// /\\n}"

Let me share the next level up challenge first!
:)
No...my bash is very very basic. I just use it to deploy nia. I just googled and realized can use brace expansion
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
No...my bash is very very basic. I just use it to deploy nia. I just googled and realized can use brace expansion
Good enough, you know how to find your ways around. Between what you have eventually provided and what I have conjure, we can see different approaches to solving the newline issue, right ? Nice!
:)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
NEXT BETTER CHALLENGER: BASHING THE FIBONACCI SEQUENCE!

There are 2 similarly different Fibonacci sequences. We will stick with the classic one where Term #0 will start with 0, as such the Fibonacci sequence we are going to create using BASH will be as follows:
0, 1, 1, 2, 3, 5, 8, 13, ...

So far so good right ?
Using the same set of constraints found in the original question above, lets have another Bash script when invoked as such

Code:
./fibo.sh N

Where input 0 <= N <= 92 (I will let you find out why the max is only 92)

Output the Fibonacci sequence from Term #0 to Term #N, each term separately on each line.
Below is an example of a Fibonacci sequence from Term #0 to Term #N
Code:
$ ./fibo.sh 0
0
$ ./fibo.sh 1
0
1
$ ./fibo.sh 2
0
1
1
$ ./fibo.sh 3
0
1
1
2
$ ./fibo.sh 4
0
1
1
2
3
$ ./fibo.sh 5
0
1
1
2
3
5
$ ./fibo.sh 10
0
1
1
2
3
5
8
13
21
34
55
$ ./fibo.sh 20
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

PERK TO CHALLENGERS

I will bring out one extra perk should you intend to take in input where N > 92
If your code readily accept larger input for N, you will have an exception to the use of the bc utility so that you can compute arbitrary precision integers!
I highly recommend that your solution incorporate this perk!

In this perk case, the input will be as follows
0 <= N <= 10000


Below will be an example output for fibo.sh 200 and fibo.sh 10000.
Code:
$ ./fibo.sh 200
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
...
3691087032412706639440686994833808526209
5972304273877744135569338397692020533504
9663391306290450775010025392525829059713
15635695580168194910579363790217849593217
25299086886458645685589389182743678652930
40934782466626840596168752972961528246147
66233869353085486281758142155705206899077
107168651819712326877926895128666735145224
173402521172797813159685037284371942044301
280571172992510140037611932413038677189525

$ ./fibo.sh 10000
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
...
20793608237133498072112648988642836825087036094015903119682945866528501423455686648927456034305226515591757343297190158010624794267250973176133810179902738038231789748346235556483191431591924532394420028067810320408724414693462849062668387083308048250920654493340878733226377580847446324873797603734794648258113858631550404081017260381202919943892370942852601647398213554479081823593715429566945149312993664846779090437799284773675379284270660175134664833266377698642012106891355791141872776934080803504956794094648292880566056364718187662668970758537383352677420835574155945658542003634765324541006121012446785689171494803262408602693091211601973938229446636049901531963286159699077880427720289235539329671877182915643419079186525118678856821600897520171070499437657067342400871083908811800976259727431820539554256869460815355918458253398234382360435762759823179896116748424269545924633204614137992850814352018738480923581553988990897151469406131695614497783720743461373756218685106856826090696339815490921253714537241866911604250597353747823733268178182198509240226955826416016690084749816072843582488613184829905383150180047844353751554201573833105521980998123833253261228689824051777846588461079790807828367132384798451794011076569057522158680378961532160858387223882974380483931929541222100800313580688585002598879566463221427820448492565073106595808837401648996423563386109782045634122467872921845606409174360635618216883812562321664442822952537577492715365321134204530686742435454505103269768144370118494906390254934942358904031509877369722437053383165360388595116980245927935225901537634925654872380877183008301074569444002426436414756905094535072804764684492105680024739914490555904391369218696387092918189246157103450387050229300603241611410707453960080170928277951834763216705242485820801423866526633816082921442883095463259080471819329201710147828025221385656340207489796317663278872207607791034431700112753558813478888727503825389066823098683355695718137867882982111710796422706778536913192342733364556727928018953989153106047379741280794091639429908796650294603536651238230626
33644764876431783266621612005107543310302148460680063906564769974680081442166662368155595513633734025582065332680836159373734790483865268263040892463056431887354544369559827491606602099884183933864652731300088830269235673613135117579297437854413752130520504347701602264758318906527890855154366159582987279682987510631200575428783453215515103870818298969791613127856265033195487140214287532698187962046936097879900350962302291026368131493195275630227837628441540360584402572114334961180023091208287046088923962328835461505776583271252546093591128203925285393434620904245248929403901706233888991085841065183173360437470737908552631764325733993712871937587746897479926305837065742830161637408969178426378624212835258112820516370298089332099905707920064367426202389783111470054074998459250360633560933883831923386783056136435351892133279732908133732642652633989763922723407882928177953580570993691049175470808931841056146322338217465637321248226383092103297701648054726243842374862411453093812206564914032751086643394517512161526545361333111314042436854805106765843493523836959653428071768775328348234345557366719731392746273629108210679280784718035329131176778924659089938635459327894523777674406192240337638674004021330343297496902028328145933418826817683893072003634795623117103101291953169794607632737589253530772552375943788434504067715555779056450443016640119462580972216729758615026968443146952034614932291105970676243268515992834709891284706740862008587135016260312071903172086094081298321581077282076353186624611278245537208532365305775956430072517744315051539600905168603220349163222640885248852433158051534849622434848299380905070483482449327453732624567755879089187190803662058009594743150052402532709746995318770724376825907419939632265984147498193609285223945039707165443156421328157688908058783183404917434556270520223564846495196112460268313970975069382648706613264507665074611512677522748621598642530711298441182622661057163515069260029861704945425047491378115154139941550671256271197133252763631939606902895650288268608362241082050562430701794976171121233066073310059947366875

Happy bashing to the fibonacci challengers!
:)
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
@diminishin Not so sure if your brace expansion will work for the next challenge just shared.
Welcome your participation!

Inviting @project_00 too!

:)
 
Last edited:

peterchan75

Supremacy Member
Joined
Apr 26, 2003
Messages
6,718
Reaction score
529
@peterchan75 For you, give you a break. You can solve in Perl with the same restriction. It is either you know or you don’t know. No need to think whether academia or not.

I will show you my solution in both Bash and Perl.

:)
Is that a token of appreciation for ruffling up some feathers? :oops:
Since you mention some sorting algorithm, somehow heap sort is like being kick to the curb. I remembered it was jack of all sort algorithms.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
Is that a token of appreciation for ruffling up some feathers? :oops:
Since you mention some sorting algorithm, somehow heap sort is like being kick to the curb. I remembered it was jack of all sort algorithms.

Just giving you more options since you can write Perl.

Why should heap sort be jack of all sorts? Well it is balance because of the completeness of a heap tree and that gives it a worst case of O(N.logN) time complexity due to the max height of the heap is always <= log N. But it is also an unstable sort which can be an issue in some scenarios where you want to preserve the relative order of duplicated elements.

You will also want to read
https://stackoverflow.com/questions/2467751/quicksort-vs-heapsort
Say take quicksort for example, I know it is very easy to parallelise due to the divide and conquer technique. I have not study fully, but my understanding on how the heap sort does it floating characteristics, it can be harder to parallelise if possible at all.

If not for the fact that shell script doesn’t have multi threading capabilities, it will be rather easy to modify one of my quicksort implementation in Bash written in the other thread by just using sub shell. The other one that can be parallelise won’t be any better due to excessive memory copying.

:)
 
Last edited:

diminishin

Senior Member
Joined
Sep 12, 2013
Messages
2,105
Reaction score
1,218
without using loops or recursion how to create fib seq? if i make the bash script calls itself is that considered as recursion?
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top