aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-03-26 00:02:16 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-03-26 00:02:16 +0000
commitfa0008580d9bb5aba4a15bf03d77cb184baba26c (patch)
treefa13466359173df56f99789e20db1c8674f4c909
parent4c074d22de5b502db1ee263e4d92cf6095eefe32 (diff)
downloadperlweeklychallenge-club-fa0008580d9bb5aba4a15bf03d77cb184baba26c.tar.gz
perlweeklychallenge-club-fa0008580d9bb5aba4a15bf03d77cb184baba26c.tar.bz2
perlweeklychallenge-club-fa0008580d9bb5aba4a15bf03d77cb184baba26c.zip
Add Basic solution to challenges 103, 104 and 105
-rw-r--r--challenge-103/paulo-custodio/basic/ch-1.bas33
-rw-r--r--challenge-103/paulo-custodio/basic/ch-2.bas117
-rw-r--r--challenge-104/paulo-custodio/basic/ch-1.bas33
-rw-r--r--challenge-104/paulo-custodio/basic/ch-2.bas79
-rw-r--r--challenge-105/paulo-custodio/basic/ch-1.bas27
-rw-r--r--challenge-105/paulo-custodio/basic/ch-2.bas38
6 files changed, 327 insertions, 0 deletions
diff --git a/challenge-103/paulo-custodio/basic/ch-1.bas b/challenge-103/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..6479ec41bb
--- /dev/null
+++ b/challenge-103/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,33 @@
+' Challenge 103
+'
+' TASK #1 › Chinese Zodiac
+' Submitted by: Mohammad S Anwar
+' You are given a year $year.
+'
+' Write a script to determine the Chinese Zodiac for the given year $year.
+' Please check out wikipage for more information about it.
+'
+' The animal cycle: Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey,
+' Rooster, Dog, Pig.
+' The element cycle: Wood, Fire, Earth, Metal, Water.
+'
+' Example 1:
+' Input: 2017
+' Output: Fire Rooster
+' Example 2:
+' Input: 1938
+' Output: Earth Tiger
+
+dim shared animals(11) as string
+dim shared elements(9) as string
+
+data "Rat","Ox","Tiger","Rabbit","Dragon","Snake","Horse","Goat","Monkey","Rooster","Dog","Pig"
+data "Wood","Wood","Fire","Fire","Earth","Earth","Metal","Metal","Water","Water"
+
+dim i as integer, s as string
+for i=0 to 11: read s: animals(i)=s:next
+for i=0 to 9: read s: elements(i)=s:next
+
+dim year as integer
+year=val(command(1))-1924
+print elements(year mod 10);" ";animals(year mod 12)
diff --git a/challenge-103/paulo-custodio/basic/ch-2.bas b/challenge-103/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..18a8167af6
--- /dev/null
+++ b/challenge-103/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,117 @@
+' Challenge 103
+'
+' TASK #2 › What’s playing?
+' Submitted by: Albert Croft
+' Working from home, you decided that on occasion you wanted some background
+' noise while working. You threw together a network streamer to continuously
+' loop through the files and launched it in a tmux (or screen) session, giving
+' it a directory tree of files to play. During the day, you connected an audio
+' player to the stream, listening through the workday, closing it when done.
+'
+' For weeks you connect to the stream daily, slowly noticing a gradual drift of
+' the media. After several weeks, you take vacation. When you return, you are
+' pleasantly surprised to find the streamer still running. Before connecting,
+' however, if you consider the puzzle of determining which track is playing.
+'
+' After looking at a few modules to read info regarding the media, a quick bit
+' of coding gave you a file list. The file list is in a simple CSV format, each
+' line containing two fields: the first the number of milliseconds in length,
+' the latter the media’s title (this example is of several episodes available
+' from the MercuryTheatre.info):
+'
+' 1709363,"Les Miserables Episode 1: The Bishop (broadcast date: 1937-07-23)"
+' 1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)"
+' 1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)"
+' 1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)"
+' 1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)"
+' 1714640,"Les Miserables Episode 6: The Barricade (broadcast date: 1937-08-27)"
+' 1714640,"Les Miserables Episode 7: Conclusion (broadcast date: 1937-09-03)"
+' For this script, you can assume to be provided the following information:
+'
+' * the value of $^T ($BASETIME) of the streamer script,
+' * the value of time(), and
+' * a CSV file containing the media to play consisting of the length in
+' milliseconds and an identifier for the media (title, filename, or other).
+' Write a program to output which file is currently playing. For purposes of
+' this script, you may assume gapless playback, and format the output as you
+' see fit.
+'
+' Optional: Also display the current position in the media as a time-like value.
+'
+' Example:
+'
+' Input: 3 command line parameters: start time, current time, file name
+'
+' # Streamer start time: Tue Nov 23 12:22:03 2020
+' 1606134123
+'
+' # Current time: Mon Mar 1 09:34:36 2021
+' 1614591276
+'
+' # filelist.csv
+'
+' Output:
+'
+' "Les Miserables Episode 1: The Bishop (broadcast date: 1937-07-23)"
+' 00:17:33
+
+type TrackType
+ secs as integer
+ title as string
+end type
+
+redim shared tracks(1) as TrackType
+
+sub read_tracks(filename as string)
+ dim fn as integer, text as string, i as integer, p1 as integer, p2 as integer
+
+ fn=FreeFile
+ open filename for input as #fn
+ do while not eof(fn)
+ line input #fn, text
+ if i>ubound(tracks) then redim preserve tracks(i)
+ tracks(i).secs=val(text)/1000
+ p1=instr(text,""""): p2=instr(p1+1,text,"""")
+ tracks(i).title=mid(text,p1+1,p2-p1-1)
+ i=i+1
+ loop
+end sub
+
+function tracks_secs() as integer
+ dim i as integer, secs as integer
+
+ for i=0 to ubound(tracks)
+ secs=secs+tracks(i).secs
+ next
+ tracks_secs=secs
+end function
+
+sub show_track(secs as integer)
+ dim i as integer, hh as integer, mm as integer, ss as integer
+
+ secs=secs mod tracks_secs()
+ for i=0 to ubound(tracks)
+ if secs<tracks(i).secs then ' playing this title
+ hh=int(secs/3600): secs=secs mod 3600
+ mm=int(secs/60): secs=secs mod 60
+ ss=secs
+
+ print tracks(i).title
+ print right("00"+trim(str(hh)),2);":";
+ print right("00"+trim(str(mm)),2);":";
+ print right("00"+trim(str(ss)),2)
+ exit sub
+ else
+ secs=secs-tracks(i).secs
+ end if
+ next
+end sub
+
+' main
+dim start_secs as integer, current_secs as integer, filename as string
+start_secs=val(command(1))
+current_secs=val(command(2))
+filename=command(3)
+
+read_tracks filename
+show_track current_secs-start_secs
diff --git a/challenge-104/paulo-custodio/basic/ch-1.bas b/challenge-104/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..d9b758a281
--- /dev/null
+++ b/challenge-104/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,33 @@
+' Challenge 104
+'
+' TASK #1 › FUSC Sequence
+' Submitted by: Mohammad S Anwar
+' Write a script to generate first 50 members of FUSC Sequence. Please refer to
+' OEIS for more information.
+'
+' The sequence defined as below:
+'
+' fusc(0) = 0
+' fusc(1) = 1
+' for n > 1:
+' when n is even: fusc(n) = fusc(n / 2),
+' when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2)
+
+function fusc(n as integer) as integer
+ if n=0 then
+ fusc=0
+ elseif n=1 then
+ fusc=1
+ elseif (n and 1)=0 then
+ fusc=fusc(n/2)
+ else
+ fusc=fusc((n-1)/2)+fusc((n+1)/2)
+ end if
+end function
+
+dim i as integer, n as integer
+n=val(command(1))
+for i=0 to n-1
+ print trim(str(fusc(i)));" ";
+next
+print
diff --git a/challenge-104/paulo-custodio/basic/ch-2.bas b/challenge-104/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..24a283d74f
--- /dev/null
+++ b/challenge-104/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,79 @@
+' Challenge 104
+'
+' TASK #2 › NIM Game
+' Submitted by: Mohammad S Anwar
+' Write a script to simulate the NIM Game.
+'
+' It is played between 2 players. For the purpose of this task, let assume you
+' play against the machine.
+'
+' There are 3 simple rules to follow:
+'
+' a) You have 12 tokens
+' b) Each player can pick 1, 2 or 3 tokens at a time
+' c) The player who picks the last token wins the game
+'
+' A plays to win, B plays randomly, A wins 91% of the time
+' If both play to win, the second player to play always wins
+
+const matches as integer = 100000
+
+function play_to_win(T as integer) as integer
+ if T <= 3 then
+ play_to_win=T
+ elseif (T mod 4)=0 then
+ play_to_win=1
+ elseif (T mod 5)=0 then
+ play_to_win=1
+ elseif (T mod 6)=0 then
+ play_to_win=2
+ elseif (T mod 7)=0 then
+ play_to_win=3
+ elseif (T mod 9)=0 then
+ play_to_win=2
+ elseif (T mod 11)=0 then
+ play_to_win=2
+ else
+ error 1
+ end if
+end function
+
+function rand_play(T as integer) as integer
+ return int(rnd*3)+1
+end function
+
+sub play(is_rand as boolean)
+ dim winsA as integer, winsB as integer, i as integer, T as integer, move as integer
+
+ for i=1 to matches
+ T = 12
+ do while T > 0
+ ' player A
+ move = play_to_win(T)
+ T = T - move
+ if T <= 0 then
+ winsA = winsA+1
+ exit do
+ end if
+ ' player B
+ if is_rand then
+ move = rand_play(T)
+ else
+ move = play_to_win(T)
+ end if
+ T = T - move
+ if T <= 0 then
+ winsB = winsB+1
+ exit do
+ end if
+ loop
+ next
+
+ print "A wins ";int((winsA)/(winsA+winsB)*100);"% of the matches."
+end sub
+
+if command(1)="random" then
+ play true
+else
+ play false
+end if
diff --git a/challenge-105/paulo-custodio/basic/ch-1.bas b/challenge-105/paulo-custodio/basic/ch-1.bas
new file mode 100644
index 0000000000..790f9314b9
--- /dev/null
+++ b/challenge-105/paulo-custodio/basic/ch-1.bas
@@ -0,0 +1,27 @@
+' Challenge 105
+'
+' TASK #1 › Nth root
+' Submitted by: Mohammad S Anwar
+' You are given positive numbers $N and $k.
+'
+' Write a script to find out the $Nth root of $k. For more information, please
+' take a look at the wiki page.
+'
+' Example
+' Input: $N = 5, $k = 248832
+' Output: 12
+'
+' Input: $N = 5, $k = 34
+' Output: 2.02
+
+const ROUND_FACTOR as double = 10000.0
+
+function round(n as double) as double
+ round = int(n*ROUND_FACTOR+0.5)/ROUND_FACTOR
+end function
+
+dim n as integer, k as integer
+
+n=val(command(1))
+k=val(command(2))
+print trim(str(round(k^(1/n))))
diff --git a/challenge-105/paulo-custodio/basic/ch-2.bas b/challenge-105/paulo-custodio/basic/ch-2.bas
new file mode 100644
index 0000000000..11bd8d95ef
--- /dev/null
+++ b/challenge-105/paulo-custodio/basic/ch-2.bas
@@ -0,0 +1,38 @@
+' Challenge 105
+'
+' TASK #2 › The Name Game
+' Submitted by: Mohammad S Anwar
+' You are given a $name.
+'
+' Write a script to display the lyrics to the Shirley Ellis song The Name Game.
+' Please checkout the wiki page for more information.
+'
+' Example
+' Input: $name = "Katie"
+' Output:
+'
+' Katie, Katie, bo-batie,
+' Bonana-fanna fo-fatie
+' Fee fi mo-matie
+' Katie!
+
+sub print_song(sName as string)
+ dim sEnd as string, b as string, f as string, m as string
+
+ sEnd = lcase(sName)
+ if sEnd>="b" and sEnd<="z" _
+ and left(sEnd,1)<>"e" _
+ and left(sEnd,1)<>"i" _
+ and left(sEnd,1)<>"o" _
+ and left(sEnd,1)<>"u" then sEnd=mid(sEnd,2)
+ if left(sName,1)<>"B" then b="b"
+ if left(sName,1)<>"F" then f="f"
+ if left(sName,1)<>"M" then m="m"
+
+ print sName;", ";sName;", bo-";b;sEnd;","
+ print "Bonana-fanna fo-";f;sEnd
+ print "Fee fi mo-";m;sEnd
+ print sName;"!"
+end sub
+
+print_song command(1)