Skip to content

Commit c99b7be

Browse files
committed
Completed the introductory language problems
1 parent e33918b commit c99b7be

File tree

6 files changed

+171
-1
lines changed

6 files changed

+171
-1
lines changed

squirrel/anagrams.nut

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function join(a, separator = "")
2+
{
3+
return a.reduce(@(str1, str2) str1 + separator + str2);
4+
}
5+
6+
function printAnagrams(a, n)
7+
{
8+
if (n == 0)
9+
{
10+
print(join(a) + "\n")
11+
}
12+
else
13+
{
14+
for (local i = 0 ; i < n ; i++)
15+
{
16+
printAnagrams(a, n-1)
17+
local j = n % 2 == 0 ? 0 : i
18+
local temp = a[j]
19+
a[j] = a[n]
20+
a[n] = temp
21+
}
22+
printAnagrams(a, n-1)
23+
}
24+
}
25+
26+
// Builds the input from stdin.
27+
local word = []
28+
foreach (byte in stdin.readblob(stdin.len()-2))
29+
{
30+
word.push(byte.tochar())
31+
if (byte == 32) {
32+
throw "Exactly one argument is needed."
33+
}
34+
}
35+
printAnagrams(word, word.len()-1)

squirrel/floating_point_mystery.nut

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@ print((0.001 + 0.002 == 0.003) + "\n") // Outputs true
44
print((0.0001 + 0.0002 == 0.0003) + "\n") // Outputs false
55
print((0.00001 + 0.00002 == 0.00003) + "\n") // Outputs true
66
print((0.000001 + 0.000002 == 0.000003) + "\n") // Outputs true
7-
print((0.0000001 + 0.0000002 == 0.0000003) + "\n") // Outputs true
7+
print((0.0000001 + 0.0000002 == 0.0000003) + "\n") // Outputs true
8+
print((0.00000001 + 0.00000002 == 0.00000003) + "\n") // Outputs true
9+
print((0.000000001 + 0.000000002 == 0.000000003) + "\n") // Outputs false
10+
print((0.0000000001 + 0.0000000002 == 0.0000000003) + "\n") // Outputs true
11+
print((0.00000000001 + 0.00000000002 == 0.00000000003) + "\n") // Outputs true
12+
print((0.000000000001 + 0.000000000002 == 0.000000000003) + "\n") // Outputs true
13+
print((0.0000000000001 + 0.0000000000002 == 0.0000000000003) + "\n") // Outputs true
14+
print((0.00000000000001 + 0.00000000000002 == 0.00000000000003) + "\n") // Outputs true
15+
print((0.000000000000001 + 0.000000000000002 == 0.000000000000003) + "\n") // Outputs false
16+
print((0.0000000000000001 + 0.0000000000000002 == 0.0000000000000003) + "\n") // Outputs true

squirrel/permutations.nut

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function join(a, separator = "")
2+
{
3+
return a.reduce(@(str1, str2) str1 + separator + str2);
4+
}
5+
6+
function printPermutations(a, n)
7+
{
8+
if (n == 0)
9+
{
10+
print(join(a, "\t") + "\n")
11+
}
12+
else
13+
{
14+
for (local i = 0 ; i < n ; i++)
15+
{
16+
printPermutations(a, n-1)
17+
local j = n % 2 == 0 ? 0 : i
18+
local temp = a[j]
19+
a[j] = a[n]
20+
a[n] = temp
21+
}
22+
printPermutations(a, n-1)
23+
}
24+
}
25+
26+
// Check stdin if it's empty.
27+
if (stdin.len() == 2)
28+
{
29+
throw "At least one argument is needed."
30+
}
31+
32+
// Builds the input from stdin.
33+
local input = ""
34+
foreach (byte in stdin.readblob(stdin.len()-2))
35+
{
36+
input += byte.tochar()
37+
}
38+
39+
local phrase = split(input, " ")
40+
printPermutations(phrase, phrase.len()-1)

squirrel/test.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ function Assert-MatchTests {
99
}
1010

1111
$Error.clear()
12+
"rats" | sq "$PSScriptRoot\anagrams.nut" |
13+
Compare-Object (Get-Content "$PSScriptRoot\..\test\rats_heap_expected") |
14+
Assert-MatchTests &&
1215
sq "$PSScriptRoot\clockhands_time.nut" |
1316
Compare-Object (Get-Content "$PSScriptRoot\..\test\clockhands_expected") |
1417
Assert-MatchTests &&
@@ -17,9 +20,20 @@ sq "$PSScriptRoot\clockhands.nut" |
1720
Assert-MatchTests &&
1821
sq "$PSScriptRoot\floating_point_mystery.nut" &&
1922
sq "$PSScriptRoot\hello.nut" &&
23+
"I like carrots" | sq "$PSScriptRoot\permutations.nut" |
24+
Compare-Object (Get-Content "$PSScriptRoot\..\test\carrots_expected") |
25+
Assert-MatchTests &&
26+
Get-Content "$PSScriptRoot\..\test\wnba_input" |
27+
sq "$PSScriptRoot\top_ten_scorers.nut" |
28+
Compare-Object (Get-Content "$PSScriptRoot\..\test\wnba_expected") |
29+
Assert-MatchTests &&
2030
sq "$PSScriptRoot\triple.nut" |
2131
Compare-Object (Get-Content "$PSScriptRoot\..\test\triple_expected") |
2232
Assert-MatchTests &&
33+
Get-Content "$PSScriptRoot\..\test\wordcount_ascii_input" |
34+
sq "$PSScriptRoot\wordcount.nut" |
35+
Compare-Object (Get-Content "$PSScriptRoot\..\test\wordcount_ascii_expected") |
36+
Assert-MatchTests &&
2337
ForEach-Object 'foo';
2438

2539
if ($Error -or !$?) {

squirrel/top_ten_scorers.nut

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Player
2+
{
3+
4+
constructor(newTeam, newName, newAverage)
5+
{
6+
team = newTeam
7+
name = newName
8+
average = newAverage
9+
}
10+
11+
function _tostring()
12+
{
13+
return format("%-22s%-4s%8.2f\n", name, team, average)
14+
}
15+
16+
team = ""
17+
name = ""
18+
average = 0.0
19+
20+
}
21+
22+
local data = ""
23+
foreach (byte in stdin.readblob(stdin.len()-2))
24+
{
25+
data += byte.tochar()
26+
}
27+
28+
local players = []
29+
foreach (line in split(data, "\n", true))
30+
{
31+
local playerInfo = split(line, ",")
32+
local team = playerInfo[0]
33+
local name = playerInfo[1]
34+
local games = playerInfo[2].tofloat()
35+
local points = playerInfo[3].tofloat()
36+
if (games >= 15)
37+
players.push(Player(team, name, points / games))
38+
}
39+
40+
foreach (i, player in players.sort(@(a, b) b.average <=> a.average))
41+
{
42+
if (i >= 10)
43+
break
44+
print(player)
45+
}

squirrel/wordcount.nut

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local input = ""
2+
foreach (byte in stdin.readblob(stdin.len()-2))
3+
{
4+
input += byte.tochar()
5+
}
6+
input = input.tolower()
7+
8+
local regex = regexp("[a-z\']+")
9+
local counts = {}
10+
local index = 0
11+
for (;;)
12+
{
13+
local res = regex.search(input, index)
14+
if (res == null)
15+
break
16+
local word = input.slice(res.begin, res.end)
17+
if (word in counts)
18+
counts[word]++
19+
else
20+
counts[word] <- 1
21+
index = res.end
22+
}
23+
24+
foreach (word in counts.keys().sort(@(a, b) a <=> b))
25+
{
26+
printf("%s %d\n", word, counts[word])
27+
}

0 commit comments

Comments
 (0)