View Full Version : alright gufmn, just you and me...
inkedmn
09-27-2002, 07:13 PM
I hereby challenge gufmn to a python duel TO THE DEATH. :)
here's the deal...
gufmn and i recently took a test in our java class. the details of the test are here (http://staffwww.fullcoll.edu/brippe/cis226/tests/Test1.htm)
gufmn (should he choose to accept this challenge) and i will be writing exactly the same program in python. it must do EVERYTHING outlined in the test description above. the winner will be determined by the cleanliness and efficiency of their code.
now, an impartial third party will have to judge this duel. who will accept this challenge?
gufmn, DO YOU ACCEPT?
:)
gufmn
09-27-2002, 07:22 PM
Is there a time frame for completion? I'm kinda buisy right now.
inkedmn
09-27-2002, 07:24 PM
i'd say by Sunday at midnight.
if you'd like more time, that's cool.
gufmn
09-27-2002, 07:41 PM
You're on! In time frame we agreed upon through IM.
/me grabs glove, smacks inkedmn across the face and proceeds to walk 10 paces.
oo We oo We oooo.....(The sun begins to set)
GnuVince
09-27-2002, 07:49 PM
Inkedmn asked me to be judge. I accept.
I just want to mention that I've got the solution implemented in Python and Ruby ;)
ChefNinja
09-28-2002, 06:13 AM
interesting :D
Strike
09-28-2002, 06:52 AM
Can I judge?
GnuVince
09-28-2002, 10:57 AM
Strike: No. I am the judge. You can write an entry though and email it to me and I'll give you a score.
GnuVince
09-28-2002, 01:07 PM
Oh by the way guys, you can send me many versions. I will correct the last one recieved before the deadline.
If Gnuvince is the judge you'll get bonus points if you write it in frenchie!!! :D
inkedmn
09-28-2002, 02:55 PM
Originally posted by Strike
Can I judge?
thanks for offering, man :)
Strike
09-28-2002, 03:33 PM
Oh I thought it was going to be a panel of judges, not just a single judge. (NOTE: not just because I want to judge do I say this, but that's a generally better idea anyway just so you can get several different perspectives and win on aggregate score)
inkedmn
09-28-2002, 03:36 PM
hmm...
not a bad idea actually. i'll have to see how gufmn feels about it...
gufmn?
jemfinch
09-28-2002, 05:46 PM
I can judge too, if you like.
Jeremy
gufmn
09-29-2002, 05:21 PM
I don't care who judges.
inkedmn: if you think it best to have multiple judges, that's cool with me. In the mean time, I'm just going to send my code to Vince. Should it be decided that more judges is the way to go, I'll send it to them as well.
GnuVince
09-29-2002, 06:10 PM
I'll wait to see inkedmn's entry before I decide if I want advise from Strike.
By the way, gufmn's entry does what is expected.
GnuVince
09-29-2002, 08:22 PM
Winner: inkedmn.
I declare inkedmn the winner of the 'inkedmn v. gufmn' duel. For one, ink's code is generally easier to read than guf's. Since, this was a Python contest and that Python is an OO language, inkedmn gets more points for having a OO design. gufmn loses a point for using a global variable. By the way gufmn, you don't need to put int() to declare an integer variable. The output on ink's program was clearer than guf's.
ink's program was also faster (printing 12 size 50 triangles):
You made 12 triangles!
python2.2 ink.py 0.04s user 0.01s system 0% cpu 20.662 total
...
Total number of triangles printed: 12
Goodbye...
python2.2 gufmn.py 0.14s user 0.02s system 0% cpu 19.236 total
This is probably due to the fact that ink used 'for' loops and guf used
'while' loops to print the triangles.
I accord a coolness point to gufmn's program for not having a main loop,
but for using recursion instead. Inkedmn also gets a coolness point for
managing plurals in his program.
Both player used meaniful variable and function names making it easier
for me to understand their programs. Also, major point to gufmn for using 4-space tabs (thus respecting the Python coding guide lines!) unlike inkedmn who still uses 8-space tabs. Good job on using the 'in' operator for the "Draw again" question too.
Big phat l33t congratulations to both participants! If other members have version (in Python or other languages), you are invited to post them.
By the way, scanez you may not cry because inkedmn won ;)
---
Here are the contestants source code:
gufmn:
numTriangles = int(0)
def main():
global numTriangles
try:
size = int(raw_input("Enter the size of the triangle to print(between 1 and 50): "))
print ""
if size >= 1 and size <= 50:
numTriangles += 1
counter = int(1)
while counter < size:
printLine(counter)
counter += 1
while size >= 1:
printLine(size)
size -= 1
playAgain()
else:
print "Entry must be between 1 and 50\n"
main()
except Exception, e:
print "Invalid entry"
main()
def printLine(x):
z = int(1)
numStars = ""
star = "*"
while z <= x:
numStars += "*"
z += 1
print numStars
def playAgain():
global numTriangles
answer = raw_input("\nWould you like to draw another?-y/n: ")
if answer in "Nn":
print "\nTotal number of triangles printed:",numTriangles
print "\nGoodbye..."
elif answer in "Yy":
main()
else:
print answer + " is not a valid entry.\n"
playAgain()
main()
inkedmn:
# the triangle duel :)
# coded by candlelight by inkedmn
# inkedmn@inkedmn.net
from string import upper
class Triangles:
def __init__(self):
self.triangles = 0
self.num = 0
def getNumber(self):
try:
self.num = int(raw_input("\nEnter the size of triangle (1-50): "))
except:
print "Invalid Entry"
self.getNumber()
if self.num < 1 or self.num > 50:
print "Invalid Entry"
self.getNumber()
def printTriangle(self, num):
self.triangles += 1
lines = (self.num * 2) - 1
bottom = self.num - 1
while lines > 0:
for i in range(self.num + 1):
print i * "*"
lines -= 1
for i in range(bottom):
print (bottom - i) * "*"
lines -= 1
def playAgain(self):
answer = raw_input("\nPlay Again? (y/n) ")
if answer.upper() == 'Y':
return 1
elif answer.upper() == 'N':
return 0
else:
print "Y or N, spanky...\n"
return self.playAgain()
def goodbye(self):
print "\nThanks for playing!\n"
if self.triangles == 1:
print "You made one triangle!"
else:
print "You made %d triangles!" % self.triangles
def main():
tri = Triangles()
quit = 0
while quit == 0:
tri.printTriangle(tri.getNumber())
if tri.playAgain() == 0:
tri.goodbye()
quit = 1
if __name__ == '__main__':
main()
---
Here are my versions; one in Python, the other in Ruby:
#!/usr/bin/env python2.2
import re
number_of_triangles = 0
continue_to_draw = 1
valid_ans = re.compile(r'^[yn]$', re.I)
while continue_to_draw:
play_again = ""
size = int(raw_input("Please type the size of the triangle: "))
if size not in range(1, 51):
print "Error: Invalid triangle size"
continue
number_of_triangles += 1
for x in xrange(1, size+1):
print "*"*x
for x in xrange(size-1, 0, -1):
print "*"*x
while not valid_ans.search(play_again):
play_again = raw_input("Would you like to print another triangle? (Y)es (N)o: ")
if not valid_ans.search(play_again):
print "Error"
if re.search(r'^n$', play_again, re.I):
continue_to_draw = 0
print "Thank you for playing the triangle game"
print "You created %d triangles" % number_of_triangles
Ruby:
#!/usr/bin/env ruby
def plural?(n)
case n
when -1..1 then ""
else "s"
end
end
def draw_triangle(size)
# Draw first half
1.upto(size-1) { |x| puts "*"*x }
# Draw second half
size.downto(1) { |x| puts "*"*x }
end
number_of_triangles = 0
continue_to_draw = true
valid_ans = /^[yn]$/i
while continue_to_draw
play_again = nil
print "Please type the size of the triangle: "
size = gets.to_i
# Error message if triangle is not the right size
if not (1..50).to_a.include?(size)
puts "Error: Invalid triangle size"
next
end
# We have a new triangle
number_of_triangles += 1
draw_triangle(size)
until play_again =~ valid_ans
print "Would you like to print another triangle? (Y)es (N)o: "
play_again = gets.chomp
if not play_again =~ valid_ans
puts "Error"
end
end
if play_again =~ /^n$/i
continue_to_draw = false
end
end
puts "Thank you for playing the triangle game"
puts "You created #{number_of_triangles} triangle#{plural?(number_of_triangles)}"
scanez
09-29-2002, 08:51 PM
sniff sniff
inkedmn: I'm so proud of you...
/me wipes a tear away
Sorry GnuVince, couldn't hold back ;)
Strike
09-29-2002, 09:13 PM
Big fat minus points for no comments or docstrings in each :P
gufmn
09-29-2002, 11:18 PM
Congrats ink!
You know I never expected to win.
Hmmm.....but I bet you did.
inkedmn's ego++
I hope you feel better ;)
inkedmn
09-29-2002, 11:19 PM
JERK!
er, thanks :)
nice job guf
Dru Lee Parsec
09-30-2002, 04:05 PM
Hey Guys, How much time were you given to write the original Java code in class?
gufmn
09-30-2002, 04:10 PM
About 2-1/2 hours total. But I lost about an hour because the computer I was using was effed. :)
Plus, I had to re-boot about an hour into it, due to the effed computer, and as it turns out, the lab machines delete all files created by students when it is shut off. So I had fun.
btw: the instructor was the one who suggested the re-boot.
Dru Lee Parsec
09-30-2002, 07:00 PM
20 minutes, all 5 task complete:
import java.awt.*;
public class Triangle {
private static EasyIn easy = new EasyIn();
private static int count = 0;
public static void main(String[] args)
{
do {
doLogicLoop();
count++;
} while (goAgain());
System.out.println("Thank you for playing the triangle game");
System.out.println("You created " + count + " triangles");
}
private static boolean goAgain() {
boolean shouldReturn = false;
boolean done = false;
do {
System.out.println("Would you like to do another triangle?");
System.out.println("(Y)es (N)o");
System.out.flush();
String shouldGo = "" + easy.readChar();
if (shouldGo.equalsIgnoreCase("Y")) {
shouldReturn = true;
done = true;
}
else if (shouldGo.equalsIgnoreCase("N")) {
shouldReturn = false;
done = true;
}
if (!done) {
System.out.print("Error: ");
}
} while (!done);
return shouldReturn;
}
private static void doLogicLoop(){
System.out.println("Please type the size of the triangle:");
System.out.flush();
int x = easy.readInt();
if ((x< 1) || (x> 50)) {
System.out.println("Error: Invalid triangle size");
count--;
}
else {
// x in in proper range so print it
// Print the triangle
// This is the first half of the triangle
for (int h = 0; h<x;h++ ) {
for (int i = 0;i <= h;i++ ) {
System.out.print("*");
}
System.out.println("");
}
// this is the 2nd half
for (int j = x-1; j>0;j-- ) {
for (int k = 0;k <j;k++ ) {
System.out.print("*");
}
System.out.println("");
}
}
}
}
gufmn
09-30-2002, 07:02 PM
^^ showoff
Dru Lee Parsec
09-30-2002, 08:35 PM
yup :D
Dru: now do it in python. :p
Kastaka
05-13-2004, 12:53 AM
Hey, I know I'm really really late, and that the contest wasn't really open, and that I'm not using python, but C++, but I figured what the hell. Maybe someone would be interested. Here is mine in C++. The comments are a little weird, I don't remember making them...
#include <iostream>
#include <ctype.h>
using namespace std;
void DrawTri(int x, int start = 1) // recursive function to draw triangles
{
int ast = 0;
do {
ast++; // increment count of *'s on current line
cout << '*'; // output *
} while(ast < start); // outputs * till limit
cout << endl; // next line
if(start < x) DrawTri(x, start+1); // recursion call
else return; // returns if limit is reached
ast = 0; // reset * count
/* this forms the rear slope of the triangle and will only
execute if the number of asterisks on the line, isn't
the limit */
do {
ast++; // increment * count on line
cout << '*'; // output *
} while(ast < start); //output * till limit
cout << endl;
}
int main()
{
int x, numtris = 0;
char choice;
do { // loop while user chooses to make more triangles(user chooses 'y')
do {
cout << "Triangle size(1 - 50): "; // prompt user for triangle size
cin >> x;
if(x < 1 || x > 50) cout << "Error: Invalid triangle size(1 - 50)\n";
} while(x < 1 || x > 50); // loop until accepted value is received
DrawTri(x); // call the function which draws the triangle
numtris++; // count number of triangles drawn
do { // loop while choice is invalid
cout << "\nDraw another triangle? (Y)es/(N)o: "; // prompt user if they wish to draw another triangle
cin >> choice;
if(tolower(choice) != 'n' && tolower(choice) != 'y')
cout << "Error: Choices are (Y)es or (N)o"; // output error if input is unacceptable
} while(tolower(choice) != 'n' && tolower(choice) != 'y');
} while(tolower(choice) != 'n');
cout << "Number of triangles made is: " << numtris; // output number of triangles drawn
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.