-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.lua
More file actions
48 lines (45 loc) · 1.58 KB
/
task.lua
File metadata and controls
48 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function splitStr(self, inSplitPattern)
local outResults = { }
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
function findSymbols(words)
local output = ''
for i = 1, #words do
local currWord = words[i]
for j = 1, currWord:len() do
local firstPos = currWord:find(currWord:sub(j,j)) --get j char from strings
if not currWord:find(currWord:sub(j,j), firstPos + 1) then --if there's no more same chars
output = output .. currWord:sub(j,j)
break
end
end
end
return output
end
local input = ''
--read lines
while true do
io.write("Input line(type Q for stop): ")
io.flush()
local str = io.read()
if str == 'Q' then break end
input = input .. str .. ' '
end
input = input:gsub('%p', ' ') --replace punctuation chars with space
input = input:gsub('%s+', ',') --replace all spaces with comma
local words = splitStr(input, ',') --split string to array
local outStr = findSymbols(words) --get string with unique chars in each word
local outChar = findSymbols({ outStr }) --get unique char from returned string
if outChar:len() == 0 then
print('There\'s no unique char')
else
print('\nUnique char is: ' .. outChar)
end