Write a program in the language of your choice that takes a string and inputs "SUSSMAN" for every time those characters appear. In case I'm not being clear, that means output "SUSSMAN" for every 3 's's and 1 'u', 'm', 'a', and 'n' in the string. Inputting "ssssumanssssssssssuman", for example, would print "SUSSMAN" twice. "ssuman", however, would print nothing.
I've written an inelegant solution in C, which I'll post in a second. It tends to crash after execution, however, and I cannot for the life of me figure out what I'm doing wrong. It does everything it's supposed to but refuses to terminate politely. Even stranger, it only happens when there is enough characters for two or more sussmen. I guess the problem is in the susscount loop, but I can't figure out where. Mind giving me a hand, /prog/?
Name:
Anonymous2010-01-15 22:58
/* SUSSMAN */
/* BEWARE, SHITTY COEDS */
#include <stdio.h>
int main()
{
char suss[1];
char letters[] = "SUMANsuman";
int count[5] = {0}; /* S, U, M, A, N */
int susscount = 0;
int x, c;
printf("Enter a string (terminate with 'Q'): ");
for (x = 0; scanf("%c", &suss[x]), suss[x] != 'Q'; x++);
for (x = 0; suss[x] != 'Q'; x++)
{
for (c = 0; c <= 9; c++)
{
if (suss[x] == letters[c])
{
if (c < 5)
count[c]++;
else
count[c-5]++;
}
}
}
while (count[0] > 0)
{
for (x = 1, c = 0; x < 5; x++)
{
if (count[x] >= 1)
c++;
count[x]--;
}
if (count[0] >= 3)
c++;
count[0] -= 3;
if (c == 5)
susscount++;
}
for (; susscount > 0; susscount--)
printf("SUSSMAN\n");
;;; Okay, so this solves a harder problem than what OP wanted. It's my fault that I misunderstood the problem.
;;; So this counts every s,u,m,a,n character, and for every 3 of them prints as many sussmans as needed
(defun count-letters (string &optional (letters "Sussman"))
(loop for letter across (remove-duplicates (string-upcase letters))
sum (truncate (count letter string :key #'char-upcase :test #'char=) 3)))
(defun print-wizardly-sussmans (string)
(loop repeat (count-letters string) do (format t "~&SUSSMAN~&")))
;;; Oh fuck, I misread the problem again, " that means output "SUSSMAN" for every 3 's's and 1 'u', 'm', 'a', and 'n' in the string."
;;; Solution should be ( reusing the original sussman-count from up there )
(defun print-wizardly-sussmans-for-real (string)
(loop repeat (sussman-count string "sssuman") do (format t "~&SUSSMAN~&")))
; That seems to be right. I hope I understood the problem correctly this last time.
I really shouldn't code while sleeping.
Name:
Anonymous2010-01-16 0:36
Just reposting your code more cleanly for comparison purposes. (defun count-letters (string &optional (letters "Sussman"))
(loop for letter across (remove-duplicates (string-upcase letters))
sum (truncate (count letter string :key #'char-upcase :test #'char=) 3)))
(defun print-wizardly-sussmans-for-real (string)
(loop repeat (sussman-count string "sssuman") do (format t "~&SUSSMAN~&")))
Name:
Anonymous2010-01-16 0:39
Damn where's the COBOL guy when you need him most?...
Name:
Anonymous2010-01-16 0:42
Actually should be
(defun sussman-count (string search-string
&aux (string (string-upcase string))
(search-string (string-upcase search-string)))
(let ((s-len (length search-string))
(b-len (length string)))
(do ((index (search search-string string)
(search search-string string :start2 index))
(i 0 (1+ i)))
((not index) i)
(when (<= (+ index s-len) b-len)
(incf index s-len)))))
(defun print-wizardly-sussmans-for-real (string)
(loop repeat (sussman-count string "sssuman") do (format t "~&SUSSMAN~&")))
but I believe your implementation is more complicated than needed, >>11-kun. "smanuss" should print a Sussman, whether the letters are in order or not. It does depend on how we interpret >>1.
I wasn't trying to write anything impressive. My goal here is to:
A) Illustrate a problem to be solved
and
B) Provide a (somewhat, in this case) working solution to the problem to prove that it can be solved
The quicker I can write a solution to the problem, the more time people have to write their own solutions, which is all that I really care about - seeing /prog/'s code. My implementation may have been ugly, but I was able to pump it out in 20 minutes, so I'm happy.
>>14
I didn't understand the exact meaning in >>1, so I wrote 3 solutions for possible meanings I thought were possible. Maybe one has the right meaning, maybe none. I'll read OP's statements more carefully tommorow and see if they match my understanding or not. sussman-count is indeed more complicated than it should be, it's pretty low level. It can be written more elegantly for sure.
Name:
Anonymous2010-01-16 1:21
>>15 I wasn't trying to write anything impressive.
You certainly accomplished that.
Protip: C doesn't have variable length arrays or array bounds checking. Your scanf loop is writing all over random memory, and it's a miracle it doesn't crash right there (though it will if you input longer strings).
I'm a dipshit, you fixed it. I never realized that C didn't have variable length arrays. In fact, I've even thought on several occasions that "fixed length arrays would be much easier to work with, why would a supposedly low-level language allow this, etc," never managing to make the connection that the language was just letting me get away with it the whole time. Thank you for explaining what my mistake was, I would have kept doing it if you hadn't explained that.
I'm not an experienced C programmer, if you couldn't tell; I'm just picking this stuff up as I go along.
>>17
What is your definition of a variable length array? You can just malloc your array as you please. The array size is static, but you can extend(re-alloc) the array if needed. Some C functions are unsafe as they can write to a buffer without having a way to control how much they would write, or if they should re-allocate a new buffer. IMO, it's much simpler and safer if the callee allocates the buffer for you and maybe provides some length metadata on the side. It's possible to design safe APIs for C and use C safely, but the coder needs to make a conscious effort to do so. (Or the coder could just use something more high-level than C...)
Name:
Anonymous2010-01-16 3:38
>>17 Protip: C doesn't have variable length arrays
Yes, it does.
Name:
clever guy2010-01-16 3:58
s,u,m,a,n = 0,0,0,0,0
local line = io.read()
for i = 1, #line do
local letter = line:sub(i, i)
if _G[letter] then _G[letter] = _G[letter] + 1 end
end
for i = 1, math.min(s / 3, u, m, a, n) do print"Sussman" end
Obviously, using lua made it too easy. I might do one in C or something later.
Name:
Anonymous2010-01-16 4:07
>>18
YHBT. Good luck with C, it's a painful language to program in, especially at first, but necessary. Avoid scanf, it's unsafe and not needed here. Just use getchar and loop while( c != '\n' ). >>16
Oh I know, I didn't correct anything in your code, I was just reposting it cleanly because the noise impeded my reading. It's a different interpretation of OP from >>9 but not wrong per se. >>20 realloc my anus
a /// b = minimum $ map (uncurry $ (quot . find a)) b
rearrange src pat = unwords . take n $ repeat pat
where n = count src /// count pat
main = putStrLn . flip rearrange "SUSSMAN" =<< getLine
Gotta write a C one next; does C do TCO?
Name:
Anonymous2010-01-16 14:25
My C version: #include <stdio.h>
int main(int argc, char **argv)
{
int s, u, m, a, n, pos;
char c;
char cs[] = "SUSSMAN ";
int* vs[] = {&s, &u, &s, &s, &m, &a, &n, &pos, 0};
while ((c = getchar()) != '\n')
{
switch (c)
{
case 's': case 'S': ++s; break;
case 'u': case 'U': ++u; break;
case 'm': case 'M': ++m; break;
case 'a': case 'A': ++a; break;
case 'n': case 'N': ++n; break;
}
while (*vs[pos])
{
*vs[pos]--;
printf("%c", cs[pos++]);
pos &= 7;
}
}
printf("\n");
return 0;
}