This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils | |
proc isIsogram*(s: string): bool = | |
result = true | |
for ch in s.toLowerAscii: | |
if ch in Letters: | |
if count(s.toLowerAscii, ch) >= 2: return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils | |
proc abbreviate*(s: string): string = | |
var str = s.replace(" -", " ") | |
str = str.replace("_") | |
str = str.replace("-", " ") | |
str = str.replace(" ", " ") | |
for word in str.split(" "): | |
result &= word[0].toUpperAscii |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc distance*(a, b: string): int = | |
if a.len != b.len: | |
raise (ref ValueError)(msg: "out of bounds") | |
else: | |
for i in low(a)..high(a): | |
if a[i] != b[i]: inc(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc sum*(limit: int, factors: openArray[int]): int = | |
for i in 0..<limit: | |
for num in factors: | |
if num != 0 and i mod num == 0: | |
result += i | |
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
Allergen* = enum | |
Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats | |
proc isAllergicTo*(score: int, allergen: Allergen): bool = | |
let allergenScore = 1 shl ord(allergen) | |
return (score and allergenScore) != 0 | |
proc allergies*(score: int): set[Allergen] = | |
result = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils, unicode | |
proc hey*(s: string): string = | |
if s == s.toUpperAscii and contains(s, Letters.set) and s[^1] == '?': | |
return "Calm down, I know what I'm doing!" | |
elif s == s.toUpperAscii and contains(s, Letters.set): | |
return "Whoa, chill out!" | |
elif not contains(s, {'A'..'Z', 'a'..'z', '0'..'9', '?'}) or s == nil: | |
return "Fine. Be that way!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc isLeapYear*(year: int): bool = | |
if year mod 100 == 0: result = year mod 400 == 0 | |
else: result = year mod 4 == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils | |
proc score*(word: string): int = | |
for char in word.toUpperAscii: | |
case char | |
of 'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T': inc(result) | |
of 'D', 'G': inc(result, 2) | |
of 'B', 'C', 'M', 'P': inc(result, 3) | |
of 'F', 'H', 'V', 'W', 'Y': inc(result, 4) | |
of 'K': inc(result,5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc twoFer*(name = ""): string = | |
if name.len > 0: return "One for " & name & ", one for me." | |
else: return "One for you, one for me." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils | |
proc isPangram*(s: string): bool = | |
for char in 'a'..'z': | |
if not contains(s.toLowerAscii, char): return false | |
else: result = true |
NewerOlder