Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Programming With Python: HELP!!!  (Read 2334 times)

Shikogan

  • Bay Watcher
  • Comically insane
    • View Profile
Programming With Python: HELP!!!
« on: March 09, 2009, 12:24:21 am »

i'm taking part in a university course where we are expected to solve a set of questions each week online. i was wondering if anyone here could help me with this weeks questions, which due today i really should have thought of something like this a while ago.

Question 1

Part 1
Create a procedure called printSquareStar(), using printNString(string, numTimes), that prints the following using printNString.

****
****
****
****


This is a 4x4 square made up of the star (*) character.

NOTE: This part is done

Part 2
Create a procedure printStarRectangle(height, width) (where string is a string and height is an integer) that prints a rectangle height units high and width units wide using '*'s. You should use printNString.

Example: printStarRectangle(5,3) =>

***
***
***
***
***

NOTE: This part is done

Part 3
Create a method called printTriangle(string, height) (where string is a string and height is an integer) that prints a right angled triangle height units high using string as the character string.

Example:
printTriangle('*', 2) =>

*
**


printTriangle('a', 3) =>

a
aa
aaa


Part 4
The goal of this exercise is to write a function that can draw two different types of shapes. Create a method printShape(shape, string, height) (where shape is a string, string is a string and height is an integer) that prints either a triangle or a square to the console depending upon the shape string. The variable shape will equal to 'square' when you are to print a square and otherwise it will be 'triangle'.

For the purpose of this exercise you may assume a printSquare(string, height) function and a printTriangle(string, height) function exist.

Question 2
Define a procedure p2(n) that takes an integer parameter n. If n is greater than 1, the procedure returns the largest power of two that is less than n; otherwise, it returns 0.
Hint: You will need to use a while loop. Here is an example of a function that adds the numbers from 1 to n

def sumto(n):
    sum = 0
    m = 1
    while m <= n:
        sum = sum + m
   m = m + 1
    return sum

For your problem you will need to take increasing powers of 2 until that power exceeds n.
Logged
Arguing on the internet is like running in the special Olympics; even if you win you're still retarded.

The Moonlit Knight

  • Bay Watcher
    • View Profile
Re: Programming With Python: HELP!!!
« Reply #1 on: March 09, 2009, 01:43:18 am »

For part 3, I would do something along the lines of:

Code: [Select]
def printTriangle(string, height):
    width = 1
    while width <= height:
        print(width * string)
        width = width + 1
Breakdown:
width starts at 1.
while width <= height makes it so the following loop only runs so long as width is less than or equal to height.
print(width * string) prints, on a new line, a string consisting of the variable string repeated width times.
width = width + 1 adds 1 to width every time the loop repeats, making it so string is, in every line, repeated once more than in the last. The less than/equal to condition in the loop will stop the loop once there have been height lines printed.

For part 4, I would use a simple if/elseif condition; if shape = "square", call the printSquare function; elseif shape = "triangle", call the printTriangle function. You might also want to toss in an else condition to return a user-friendly error if something besides "square" or "triangle" is entered for shape, but that doesn't seem necessary by the problem's definition.

My brain is too tired for Question 2, sadly. Hope I helped a bit.
Logged
A man once said to me, "If you want superpowers, all you have to do is take acid and you'll have any superpower you want." He then asked if I had just, at that moment, shaved half my face; I revealed that as being my superpower. Being suddenly and inexplicably half-shaven.

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming With Python: HELP!!!
« Reply #2 on: March 09, 2009, 07:07:14 am »

C answer:
Spoiler (click to show/hide)

Of course, that's in C, but in my experience it should be easy to transfer C to anything.
Logged
Eh?
Eh!

Derakon

  • Bay Watcher
    • View Profile
Re: Programming With Python: HELP!!!
« Reply #3 on: March 09, 2009, 07:06:49 pm »

This really sounds like you're asking us to do your homework for you. Why don't you state what you've tried doing and where you ran into trouble, so we can help you with just those specific parts instead of just handing solutions to you on a silver platter?
Logged
Jetblade - an open-source Metroid/Castlevania game with procedurally-generated levels

Shikogan

  • Bay Watcher
  • Comically insane
    • View Profile
Re: Programming With Python: HELP!!!
« Reply #4 on: March 09, 2009, 11:07:28 pm »

Derakon, it was just i had no idea on how to do any of it, and i was asking how people would go about doing it, I learn mostly by seeing so i need examples, and thanks guys for your help, i may be back if i can't figure any more out
Logged
Arguing on the internet is like running in the special Olympics; even if you win you're still retarded.

Derakon

  • Bay Watcher
    • View Profile
Re: Programming With Python: HELP!!!
« Reply #5 on: March 10, 2009, 02:32:46 pm »

If you're completely at sea, then you need to contact your instructor.

I don't want to discourage you from asking us for help, but just posting your homework assignment and saying "What do I do?" is not the right way to do it. The right way is, first off, to only post one problem at a time. Then, say what you've tried doing, and where you ran into problems, so that we can give you detailed advice that's just enough to let you finish it on your own.

Good luck with your homework. If you have specific questions, feel free to ask.
Logged
Jetblade - an open-source Metroid/Castlevania game with procedurally-generated levels

Capntastic

  • Bay Watcher
  • Greetings, mortals!
    • View Profile
    • A review and literature weblog I never update
Re: Programming With Python: HELP!!!
« Reply #6 on: March 12, 2009, 02:59:37 am »

In my bits of self-teaching, I've learned that the most important programming skill is learning to find answers from existing sources, and experimenting on your own until you get it right.   If you can't be bothered to do that for simple things, complex things are going to grind you to paste.
Logged