Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shortest code 2

Status
Not open for further replies.

tytus2005

Programmer
Jul 5, 2005
16
PL
Hi,

I'm learning Perl and for now I'm on regexp. With my friend, we make some challnages for shortest code. We have such a task: We are given some words (of length at most 20). These words are made only from capital letters (A..Z). We want to count and print out 2**(number of letters A,B,C,D) for each line in the input (in separate lines).

Example:
AFFFBFFFFC
Answer: As there are 3 letters that interest us (A,B,C) the answer is 2**3=8, and 8 should be printed on output.

I've made it on 27 characters (counting also whitespaces), but my friends claims that it can be done on 24 chars. Programs consits of only code (no #!/usr/bin/perl needed) - they are run:
perl program.pl < input.txt

Bigger example:
Input:
AAAXXXXYYYYY
BBCCSSSSSSS

Output:
8
16

Input consist always from 10 lines.
 
I don't know if this will work for you, but if you run it with
Code:
perl -l code.pl input.txt
(that's an L (el) not a 1 (one)).

code.pl:
Code:
print 2**y/ABCD//for<>
 
Or if you run the script with:
Code:
perl -ln code.pl input.txt
code.pl can look like
Code:
print 2**y/ABCD//
 
does the print need whitespace, if followed by a digit?
no idea, just guessing where the last character went ...

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
yup - print2 is a valid sub name.

f

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Thanks a lot... But now I have a real challange...

We are given a set of numbers:
Code:
x
y_1
y_2
y_3
...
y_x
y_x+1
y_x+2
...
y_n
We have to write program that will read only x first values from the input and sum up only those which are greater then zero. For example:
Code:
5
4
3
-3
-1
4
-8
12
11
10
5
6
7
2
3
Output:
Code:
11
we read only 5 first values:
Code:
4,3,-3,-1,4
and sum up only positive of them:
Code:
4+3+4=11

My best try is 43 chars... My friend made it in 10 chars... Any idea how to write it in 10 chars...
 
10 characters without any command-line options??? Have you seen the code?
 
'print ' is six and you need that for output. '<>' is another two and you need them for input. You're saying he does the maths in just two characters?

f

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
I don't know... I haven't seen the code...

BTW: We do not count characters with ASCII code less or equal to 32...

I don't know if it makes much difference...

Do you think it's impossible to do it in just 10 chars (with ASCII code>32)?
 
Why do you need to do this?

Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Code:
perl a.pl
;-)

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top