2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,3 +1,7 @@
Show how to spell out a number in English. <br>
You can use a preexisting implementation or roll your own, but you should support inputs up to at least one million (or the maximum value of your language's default bounded integer type, if that's less). <br>
;Task:
Show how to spell out a number in English.
You can use a preexisting implementation or roll your own, but you should support inputs up to at least one million (or the maximum value of your language's default bounded integer type, if that's less).
Support for inputs other than positive integers (like zero, negative integers, and floating-point numbers) is optional.
<br><br>

View file

@ -0,0 +1,131 @@
function Get-NumberName
{
<#
.SYNOPSIS
Spells out a number in English.
.DESCRIPTION
Spells out a number in English in the range of 0 to 999,999,999.
.NOTES
The code for this function was copied (almost word for word) from the C#
example on this page to show how similar Powershell is to C#.
.PARAMETER Number
One or more integers in the range of 0 to 999,999,999.
.EXAMPLE
Get-NumberName -Number 666
.EXAMPLE
Get-NumberName 1, 234, 31337, 987654321
.EXAMPLE
1, 234, 31337, 987654321 | Get-NumberName
#>
[CmdletBinding()]
[OutputType([string])]
Param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateRange(0,999999999)]
[int[]]
$Number
)
Begin
{
[string[]]$incrementsOfOne = "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
[string[]]$incrementsOfTen = "", "", "twenty", "thirty", "fourty",
"fifty", "sixty", "seventy", "eighty", "ninety"
[string]$millionName = "million"
[string]$thousandName = "thousand"
[string]$hundredName = "hundred"
[string]$andName = "and"
function GetName([int]$i)
{
[string]$output = ""
if ($i -ge 1000000)
{
$remainder = $null
$output += (ParseTriplet ([Math]::DivRem($i,1000000,[ref]$remainder))) + " " + $millionName
$i = $remainder
if ($i -eq 0) { return $output }
}
if ($i -ge 1000)
{
if ($output.Length -gt 0)
{
$output += ", "
}
$remainder = $null
$output += (ParseTriplet ([Math]::DivRem($i,1000,[ref]$remainder))) + " " + $thousandName
$i = $remainder
if ($i -eq 0) { return $output }
}
if ($output.Length -gt 0)
{
$output += ", "
}
$output += (ParseTriplet $i)
return $output
}
function ParseTriplet([int]$i)
{
[string]$output = ""
if ($i -ge 100)
{
$remainder = $null
$output += $incrementsOfOne[([Math]::DivRem($i,100,[ref]$remainder))] + " " + $hundredName
$i = $remainder
if ($i -eq 0) { return $output }
}
if ($output.Length -gt 0)
{
$output += " " + $andName + " "
}
if ($i -ge 20)
{
$remainder = $null
$output += $incrementsOfTen[([Math]::DivRem($i,10,[ref]$remainder))]
$i = $remainder
if ($i -eq 0) { return $output }
}
if ($output.Length -gt 0)
{
$output += " "
}
$output += $incrementsOfOne[$i]
return $output
}
}
Process
{
foreach ($n in $Number)
{
[PSCustomObject]@{
Number = $n
Name = GetName $n
}
}
}
}
1, 234, 31337, 987654321 | Get-NumberName

View file

@ -0,0 +1,10 @@
select val, to_char(to_date(val,'j'),'jsp') name
from
(
select
round( dbms_random.value(1, 5373484)) val
from dual
connect by level <= 5
);
select to_char(to_date(5373485,'j'),'jsp') from dual;