|
Post by Josh on Jun 10, 2017 19:00:06 GMT
How many are there? In this puzzle, you must write a program that takes the string "Hello_world,_this_is_a_String s" and outputs "3". Your program should output this because there are 3 'S's in the first part of the string. Your program should not be case sensitive.
Another example string is "abcabcabc a". This will also output 3 because there are three 'a's in the first section of the string (part before space).
The section after the space is the character you should be looking for.
To start, it's recommended that you split the string into two strings using the space.
You must not hardcode your solution.
Feel free to golf your code. If you do so, please provide the amount of characters in your code. Make sure to let us know the language used.
Good luck!
|
|
|
Post by TakeALookInTheMirror on Jun 10, 2017 19:16:51 GMT
i'm not even sure if i understood what you said but ok
a="Hello_world,_this_is_a_String s"a=a:sub(1, a:len()-2):lower()b=0 for w in a:gmatch("s") do b=b+1 end print(b)
112 characters
|
|
alu
New Member
Posts: 3
|
Post by alu on Jun 10, 2017 19:18:05 GMT
HolyC
U8 ToLower(U8 char) { if (char >= 'A' && char <= 'Z') return char + ('a' - 'A');
return char; }
I64 CountChar(U8 *str) { U8 schar = ToLower(*(str + (StrLen(str) - 1))); I64 result = 0; while (*str) { if (*str == ' ') break; if (ToLower(*str) == schar) result++; str++; } return result; } It is not necessary to split it as you said.
|
|
|
Post by Josh on Jun 10, 2017 19:33:11 GMT
Thanks for the solutions, I am aware that I didn't do a good job of explaining the puzzle. The 'first part' is the section before the space and the 'second part' is the section afterwards. The second part of the string is the character that you need to find the count of.
|
|
Hex
New Member
Posts: 19
|
Post by Hex on Jun 10, 2017 19:37:32 GMT
a=0 for i in ("Hello_world,_this_is_a_String s"):gmatch(".")do a=i:upper()=="S"and a+1 or a if i==" "then break end end print(a)
I didnt bother figuring the right pattern or splitting the string so have a very hacky solution.
|
|
lae
Active Member
Veteran
Posts: 27
ROBLOX Username: LaeMVP
|
Post by lae on Jun 10, 2017 19:39:10 GMT
function l() local num=0 for i in("Hello_world,_this_is_a_String s"):gmatch("%P+")do print(i) if i:lower():match("s")then print(i) num=num+1 end end return num end warn(l())
|
|
lae
Active Member
Veteran
Posts: 27
ROBLOX Username: LaeMVP
|
Post by lae on Jun 10, 2017 19:57:19 GMT
function l()local num=0 local s="Hello_world,_this_is_a_String s"for i in s:sub(1,#s-1):gmatch"."do if i:lower():match(s:sub(#s))then num=num+1 end end return num end warn(l()) 177 rip
|
|
Hex
New Member
Posts: 19
|
Post by Hex on Jun 10, 2017 20:00:31 GMT
a=("Hello_world,_this_is_a_string s"):upper()local_,b=a:sub(1,#a-1):gsub(a:sub(#a),"")print(b)
94 characters with length of the test data included 61 characters otherwise
|
|
john
New Member
Posts: 11
|
Post by john on Jun 10, 2017 20:49:55 GMT
#a-#a:gsub(a:sub(#a),'')
edit: in a code block:
#a-#a:gsub(a:sub(#a),'')
bytes: 24
|
|
|
Post by Bit on Jun 10, 2017 21:44:26 GMT
You called? johnYou've gotta include the string, remember that.
|
|
john
New Member
Posts: 11
|
Post by john on Jun 11, 2017 1:32:24 GMT
if that is what you want, golflua'd:
a='Hello_world,_this_is_a_String s'w(#a-#a:u():g(a:s(#a):u(),'')-1)
|
|
|
Post by cntkillme on Jun 13, 2017 23:13:43 GMT
Untested, 76 characters.
s="Hello_world,_this_is_a_String s"_,n=s:lower():gsub(s:sub(-1),s)print(n-1)
|
|