tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
120
Task/Number-names/PureBasic/number-names-1.purebasic
Normal file
120
Task/Number-names/PureBasic/number-names-1.purebasic
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
DataSection
|
||||
numberNames:
|
||||
;small
|
||||
Data.s "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"
|
||||
Data.s "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
|
||||
;tens
|
||||
Data.s "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
|
||||
;big, non-Chuquet system
|
||||
Data.s "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion"
|
||||
Data.s "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion"
|
||||
Data.s "tredecillion"
|
||||
EndDataSection
|
||||
|
||||
Procedure.s numberWords(number.s)
|
||||
;handles integers from -1E45 to +1E45
|
||||
Static isInitialized = #False
|
||||
Static Dim small.s(19)
|
||||
Static Dim tens.s(9)
|
||||
Static Dim big.s(14)
|
||||
|
||||
If Not isInitialized
|
||||
Restore numberNames
|
||||
For i = 1 To 19
|
||||
Read.s small(i)
|
||||
Next
|
||||
For i = 2 To 9
|
||||
Read.s tens(i)
|
||||
Next
|
||||
For i = 1 To 14
|
||||
Read.s big(i)
|
||||
Next
|
||||
isInitialized = #True
|
||||
EndIf
|
||||
|
||||
For i = 1 To Len(number)
|
||||
If Not FindString("- 0123456789", Mid(number,i,1), 1)
|
||||
number = Left(number, i - 1) ;trim number to the last valid character
|
||||
Break ;exit loop
|
||||
EndIf
|
||||
Next
|
||||
|
||||
Protected IsNegative = #False
|
||||
number = Trim(number)
|
||||
If Left(number,1) = "-"
|
||||
IsNegative = #True
|
||||
number = Trim(Mid(number, 2))
|
||||
EndIf
|
||||
|
||||
If CountString(number, "0") = Len(number)
|
||||
ProcedureReturn "zero"
|
||||
EndIf
|
||||
|
||||
If Len(number) > 45
|
||||
ProcedureReturn "Number is too big!"
|
||||
EndIf
|
||||
|
||||
Protected num.s = number, output.s, unit, unitOutput.s, working
|
||||
|
||||
Repeat
|
||||
working = Val(Right(num, 2))
|
||||
unitOutput = ""
|
||||
Select working
|
||||
Case 1 To 19
|
||||
unitOutput = small(working)
|
||||
Case 20 To 99
|
||||
If working % 10
|
||||
unitOutput = tens(working / 10) + "-" + small(working % 10)
|
||||
Else
|
||||
unitOutput = tens(working / 10)
|
||||
EndIf
|
||||
EndSelect
|
||||
|
||||
working = Val(Right(num, 3)) / 100
|
||||
If working
|
||||
If unitOutput <> ""
|
||||
unitOutput = small(working) + " hundred " + unitOutput
|
||||
Else
|
||||
unitOutput = small(working) + " hundred"
|
||||
EndIf
|
||||
EndIf
|
||||
|
||||
If unitOutput <> "" And unit > 0
|
||||
unitOutput + " " + big(unit)
|
||||
If output <> ""
|
||||
unitOutput + ", "
|
||||
EndIf
|
||||
EndIf
|
||||
|
||||
output = unitOutput + output
|
||||
|
||||
If Len(num) > 3
|
||||
num = Left(num, Len(num) - 3)
|
||||
unit + 1
|
||||
Else
|
||||
Break ;exit loop
|
||||
EndIf
|
||||
ForEver
|
||||
|
||||
If IsNegative
|
||||
output = "negative " + output
|
||||
EndIf
|
||||
|
||||
ProcedureReturn output
|
||||
EndProcedure
|
||||
|
||||
Define n$
|
||||
If OpenConsole()
|
||||
Repeat
|
||||
Repeat
|
||||
Print("Give me an integer (or q to quit)! ")
|
||||
n$ = Input()
|
||||
Until n$ <> ""
|
||||
|
||||
If Left(Trim(n$),1) = "q"
|
||||
Break ;exit loop
|
||||
EndIf
|
||||
PrintN(numberWords(n$))
|
||||
ForEver
|
||||
CloseConsole()
|
||||
EndIf
|
||||
48
Task/Number-names/PureBasic/number-names-2.purebasic
Normal file
48
Task/Number-names/PureBasic/number-names-2.purebasic
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
def spell_integer(n):
|
||||
tens = [None, None, "twenty", "thirty", "forty",
|
||||
"fifty", "sixty", "seventy", "eighty", "ninety"]
|
||||
|
||||
small = ["zero", "one", "two", "three", "four", "five",
|
||||
"six", "seven", "eight", "nine", "ten", "eleven",
|
||||
"twelve", "thirteen", "fourteen", "fifteen",
|
||||
"sixteen", "seventeen", "eighteen", "nineteen"]
|
||||
|
||||
bl = [None, None, "m", "b", "tr", "quadr",
|
||||
"quint", "sext", "sept", "oct", "non", "dec"]
|
||||
|
||||
def nonzero(c, n):
|
||||
return "" if n == 0 else c + spell_integer(n)
|
||||
|
||||
def big(e, n):
|
||||
if e == 0:
|
||||
return spell_integer(n)
|
||||
elif e == 1:
|
||||
return spell_integer(n) + " thousand"
|
||||
else:
|
||||
return spell_integer(n) + " " + bl[e] + "illion"
|
||||
|
||||
def base1000_rev(n):
|
||||
# generates the value of the digits of n in base 1000
|
||||
# (i.e. 3-digit chunks), in reverse.
|
||||
while n != 0:
|
||||
n, r = divmod(n, 1000)
|
||||
yield r
|
||||
|
||||
if n < 0:
|
||||
return "negative " + spell_integer(-n)
|
||||
elif n < 20:
|
||||
return small[n]
|
||||
elif n < 100:
|
||||
a, b = divmod(n, 10)
|
||||
return tens[a] + nonzero("-", b)
|
||||
elif n < 1000:
|
||||
a, b = divmod(n, 100)
|
||||
return small[a] + " hundred" + nonzero(" ", b)
|
||||
else:
|
||||
return ", ".join([big(e, x) for e, x in
|
||||
enumerate(base1000_rev(n)) if x][::-1])
|
||||
|
||||
# example
|
||||
print spell_integer(1278)
|
||||
print spell_integer(1752)
|
||||
print spell_integer(2010)
|
||||
Loading…
Add table
Add a link
Reference in a new issue