diff options
| author | Archargelod <archargelod@gmail.com> | 2024-03-01 14:43:05 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-03-01 14:43:05 +0800 |
| commit | 534715243e65728db4ee90b5ee5dee644ce71dac (patch) | |
| tree | fe17b389593130a0961afea924afc8e180fb365a /challenge-019 | |
| parent | d64cde6fdb1521b4a240e30615a90febcaf46799 (diff) | |
| download | perlweeklychallenge-club-534715243e65728db4ee90b5ee5dee644ce71dac.tar.gz perlweeklychallenge-club-534715243e65728db4ee90b5ee5dee644ce71dac.tar.bz2 perlweeklychallenge-club-534715243e65728db4ee90b5ee5dee644ce71dac.zip | |
weeks 14-26, 258 in Nim
Diffstat (limited to 'challenge-019')
| -rw-r--r-- | challenge-019/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-019/archargelod/nim/ch_1.nim | 16 | ||||
| -rwxr-xr-x | challenge-019/archargelod/nim/ch_2.nim | 40 |
3 files changed, 57 insertions, 0 deletions
diff --git a/challenge-019/archargelod/README b/challenge-019/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-019/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-019/archargelod/nim/ch_1.nim b/challenge-019/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..56eaf65648 --- /dev/null +++ b/challenge-019/archargelod/nim/ch_1.nim @@ -0,0 +1,16 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +## The only way for a month to have 5 full weekends (Fri, Sat, Sun) is +## to be 31 days long and start on Friday: +## +## .... 567 +## 1234 567 +## 1234 567 +## 1234 567 +## 1234 567 +import std/[times] + +for year in 1900..2019: + for month in Month: + if getDaysInMonth(month, year) == 31 and + getDayOfWeek(1, month, year) == dFri: + echo ($month)[0..2], '-', year diff --git a/challenge-019/archargelod/nim/ch_2.nim b/challenge-019/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..e6e72d9db2 --- /dev/null +++ b/challenge-019/archargelod/nim/ch_2.nim @@ -0,0 +1,40 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import std/[parseutils, strutils] + +proc wrap*(text: var string, lineWidth: Positive = 80) = + var spaceLeft: int = lineWidth + var index = 0 + while index < text.high: + let wordWidth = text.skipUntil(Whitespace, index) + let spaceWidth = text.skipWhile(Whitespace, index + wordWidth) + + #echo (text[index..<index + wordWidth], wordWidth + spaceWidth, spaceLeft) + if wordWidth + spaceWidth > spaceLeft: + text.insert("\n", index) + spaceLeft = lineWidth - (wordWidth + spaceWidth) + inc index # for added newLine + else: + spaceLeft -= wordWidth + spaceWidth + index += wordWidth + spaceWidth + +proc wrapped*(text: sink string, lineWidth: Positive = 80): string = + result = text + result.wrap(lineWidth) + +when isMainModule: + import std/unittest + + const + Text = """ +A simple way to do word wrapping is to use a greedy algorithm that puts as many words on a line as possible, then moving on to the next line to do the same until there are no more words left to place. This method is used by many modern word processors, such as OpenOffice.org Writer and Microsoft Word. This algorithm always uses the minimum possible number of lines but may lead to lines of widely varying lengths.""" + Expected = """ +A simple way to do word wrapping is to use a greedy algorithm that puts as many +words on a line as possible, then moving on to the next line to do the same +until there are no more words left to place. This method is used by many modern +word processors, such as OpenOffice.org Writer and Microsoft Word. This +algorithm always uses the minimum possible number of lines but may lead to +lines of widely varying lengths.""" + + suite "Greedy line wrapping": + test "wikipedia paragraph": + check Text.wrapped(80) == Expected |
