Abbreviations, automatic: Difference between revisions
Content deleted Content added
m added a related task. |
→{{header|Python}}: Added a variant (functional composition) |
||
Line 1,443: | Line 1,443: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Imperative=== |
|||
{{works with|Python|3.6}} |
{{works with|Python|3.6}} |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
Line 1,576: | Line 1,577: | ||
2 Killachau Atichau Quoyllurchau Illapachau Chaskachau Kuychichau Intichau |
2 Killachau Atichau Quoyllurchau Illapachau Chaskachau Kuychichau Intichau |
||
</pre> |
</pre> |
||
===Functional=== |
|||
<lang python>import itertools |
|||
# abbrevLen :: [String] -> Int |
|||
def abbrevLen(xs): |
|||
n = len(xs) |
|||
return next( |
|||
len(a[0]) for a in map( |
|||
lambda x: nub(map(lambda y: ''.join(y), x)), |
|||
transpose(map(inits, xs)) |
|||
) if n == len(a) |
|||
) |
|||
# TEST ---------------------------------------------------- |
|||
def main(): |
|||
xs = list(map(strip, lines( |
|||
readFile('weekDayNames.txt') |
|||
))) |
|||
for i, n in enumerate(map(compose(abbrevLen)(words), xs)): |
|||
print (n, ' ', xs[i]) |
|||
# GENERIC ------------------------------------------------- |
|||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
|||
def compose(g): |
|||
return lambda f: lambda x: g(f(x)) |
|||
# inits :: [a] -> [[a]] |
|||
def inits(xs): |
|||
return scanl(lambda a, x: a + [x])( |
|||
[] |
|||
)(list(xs)) |
|||
# lines :: String -> [String] |
|||
def lines(s): |
|||
return s.splitlines() |
|||
# nub :: [a] -> [a] |
|||
def nub(xs): |
|||
return list(dict.fromkeys(xs)) |
|||
# readFile :: FilePath -> IO String |
|||
def readFile(fp): |
|||
return open(fp).read() |
|||
# scanl is like reduce, but returns a succession of |
|||
# intermediate values, building from the left. |
|||
# scanl :: (b -> a -> b) -> b -> [a] -> [b] |
|||
def scanl(f): |
|||
return lambda a: lambda xs: ( |
|||
list(itertools.accumulate([a] + list(xs), f)) |
|||
) |
|||
# strip :: String -> String |
|||
def strip(s): |
|||
return s.strip() |
|||
# transpose :: [[a]] -> [[a]] |
|||
def transpose(xs): |
|||
return list(map(list, zip(*xs))) |
|||
# words :: String -> [String] |
|||
def words(s): |
|||
return s.split() |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{Out}} |
|||
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday |
|||
2 Sondag Maandag Dinsdag Woensdag Donderdag Vrydag Saterdag |
|||
4 E_djelë E_hënë E_martë E_mërkurë E_enjte E_premte E_shtunë |
|||
2 Ehud Segno Maksegno Erob Hamus Arbe Kedame |
|||
5 Al_Ahad Al_Ithinin Al_Tholatha'a Al_Arbia'a Al_Kamis Al_Gomia'a Al_Sabit |
|||
4 Guiragui Yergou_shapti Yerek_shapti Tchorek_shapti Hink_shapti Ourpat Shapat |
|||
2 domingu llunes martes miércoles xueves vienres sábadu |
|||
2 Bazar_gÜnÜ Birinci_gÜn Çkinci_gÜn ÜçÜncÜ_gÜn DÖrdÜncÜ_gÜn Bes,inci_gÜn Altòncò_gÜn |
|||
6 Igande Astelehen Astearte Asteazken Ostegun Ostiral Larunbat |
|||
4 Robi_bar Shom_bar Mongal_bar Budhh_bar BRihashpati_bar Shukro_bar Shoni_bar |
|||
2 Nedjelja Ponedeljak Utorak Srijeda Cxetvrtak Petak Subota |
|||
5 Disul Dilun Dimeurzh Dimerc'her Diriaou Digwener Disadorn |
|||
2 nedelia ponedelnik vtornik sriada chetvartak petak sabota</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |