June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,33 @@
String[] meaning = ["what,", "is,", "the;", "meaning,", "of:", "life."];
String[] kansas = ["we,", "are,", "not,", "in,", "kansas;", "any,", "more."];
shared void run() {
print("".join(reverseWords(meaning)));
print("".join(reverseWords(kansas)));
}
String[] reverseWords(String[] words)
=> recursiveReverseWords(words, []);
String[] recursiveReverseWords(String[] remOrig, String[] revWords)
=> if (nonempty remOrig)
then recursiveReverseWords(remOrig.rest,
revWords.withTrailing(reverseWordRecursive(remOrig.first.sequence(),
[],
revWords.size.even)))
else revWords;
String reverseWordRecursive(Character[] remOldChars, Character[] revChars, Boolean isEven)
=> if (nonempty remOldChars)
then let (char = remOldChars.first) reverseWordRecursive(remOldChars.rest,
conditionalAddChar(char, revChars, isEven),
isEven)
else String(revChars);
Character[] conditionalAddChar(Character char, Character[] chars, Boolean isEven)
=> if (isEven || isPunctuation(char))
then chars.withTrailing(char)
else chars.withLeading(char);
Boolean isPunctuation(Character char)
=> ",.:;".contains(char);

View file

@ -0,0 +1,33 @@
# io = readstring(STDIN)
io = "what,is,the;meaning,of:life."
i = 0
readbyte!() = io[global i += 1]
writebyte(c) = print(Char(c))
function odd(prev::Function = () -> false)
a = readbyte!()
if !isalpha(a)
prev()
writebyte(a)
return a != '.'
end
# delay action until later, in the shape of a closure
clos() = (writebyte(a); prev())
return odd(clos)
end
function even()
while true
c = readbyte!()
writebyte(c)
if !isalpha(c) return c != '.' end
end
end
evn = false
while evn ? odd() : even()
evn = !evn
end

View file

@ -0,0 +1,46 @@
# Project : Odd word problem
# Date : 2017/10/15
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
test = "what,is,the;meaning,of:life."
n1 = 1
testarr = []
testorigin = test
test = substr(test, ",", " ")
test = substr(test, ";", " ")
test = substr(test, ":", " ")
test = substr(test, ".", " ")
while true
n2 = substring(test, " ", n1)
n3 = substring(test, " ", n2 + 1)
if n2>0 and n3>0
strcut = substr(test, n2 + 1, n3 - n2)
strcut = trim(strcut)
if strcut != ""
add(testarr, strcut)
n1 = n3 + 1
else
exit
ok
ok
end
for n = 1 to len(testarr)
strrev = revstr(testarr[n])
testorigin = substr(testorigin, testarr[n], strrev)
next
see testorigin + nl
func Substring str,substr,n
newstr=right(str,len(str)-n+1)
nr = substr(newstr, substr)
return n + nr -1
func revstr(cStr)
cStr2 = ""
for x = len(cStr) to 1 step -1
cStr2 += cStr[x]
next
return cStr2

View file

@ -0,0 +1,35 @@
Private Function OddWordFirst(W As String) As String
Dim i As Integer, count As Integer, l As Integer, flag As Boolean, temp As String
count = 1
Do
flag = Not flag
l = FindNextPunct(i, W) - count + 1
If flag Then
temp = temp & ExtractWord(W, count, l)
Else
temp = temp & ReverseWord(W, count, l)
End If
Loop While count < Len(W)
OddWordFirst = temp
End Function
Private Function FindNextPunct(d As Integer, W As String) As Integer
Const PUNCT As String = ",;:."
Do
d = d + 1
Loop While InStr(PUNCT, Mid(W, d, 1)) = 0
FindNextPunct = d
End Function
Private Function ExtractWord(W As String, c As Integer, i As Integer) As String
ExtractWord = Mid(W, c, i)
c = c + Len(ExtractWord)
End Function
Private Function ReverseWord(W As String, c As Integer, i As Integer) As String
Dim temp As String, sep As String
temp = Left(Mid(W, c, i), Len(Mid(W, c, i)) - 1)
sep = Right(Mid(W, c, i), 1)
ReverseWord = StrReverse(temp) & sep
c = c + Len(ReverseWord)
End Function

View file

@ -0,0 +1,39 @@
Private Function OddWordSecond(Words As String) As String
Dim i&, count&, t$, cpt&, j&, l&, d&, f As Boolean
Const PUNCT As String = ",;:"
For i = 1 To Len(Words)
'first word
If i = 1 Then
cpt = 1
Do
t = t & Mid(Words, cpt, 1)
cpt = cpt + 1
Loop While InStr(PUNCT, Mid(Words, cpt, 1)) = 0 And cpt < Len(Words)
i = cpt
t = t & Mid(Words, i, 1)
End If
If Right(t, 1) = "." Then Exit For
'Odd words ==> reverse
While InStr(PUNCT, Mid(Words, cpt + 1, 1)) = 0 And cpt < Len(Words)
cpt = cpt + 1
Wend
l = IIf(f = True, i, i + 1)
d = IIf(cpt = Len(Words), cpt - 1, cpt)
For j = d To l Step -1
t = t & Mid(Words, j, 1)
Next
If cpt = Len(Words) Then t = t & ".": Exit For
f = True
i = cpt + 1
t = t & Mid(Words, i, 1)
'Even words
cpt = i + 1
t = t & Mid(Words, cpt, 1)
Do
cpt = cpt + 1
t = t & Mid(Words, cpt, 1)
Loop While InStr(PUNCT, Mid(Words, cpt, 1)) = 0 And cpt < Len(Words)
i = cpt
Next
OddWordSecond = t
End Function

View file

@ -0,0 +1,11 @@
Option Explicit
Sub Main()
Debug.Print "Input : " & "we,are;not,in,kansas;any,more."
Debug.Print "First way : " & OddWordFirst("we,are;not,in,kansas;any,more.")
Debug.Print "Second way : " & OddWordSecond("we,are;not,in,kansas;any,more.")
Debug.Print ""
Debug.Print "Input : " & "what,is,the;meaning,of:life."
Debug.Print "First way : " & OddWordFirst("what,is,the;meaning,of:life.")
Debug.Print "Second way : " & OddWordSecond("what,is,the;meaning,of:life.")
End Sub