Looking for Rekenaarwetenskap - Computer science - 113/114 test answers and solutions? Browse our comprehensive collection of verified answers for Rekenaarwetenskap - Computer science - 113/114 at stemlearn.sun.ac.za.
Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!
What does the following program print to the terminal when executed?
Wat druk die volgende program na die terminaal wanneer dit uitgevoer word?
#duplicate.pyimport stdiodef duplicate(s): t = s + s return tdef main(): s = "Hello" s = duplicate(s) t = "Bye" t = duplicate(duplicate(duplicate(t))) stdio.writeln(s+t)if __name__ == "__main__": main()
What does the following code print to the terminal when executed?
Wat druk die volgende kode na die terminaal wanneer dit uitgevoer word?
#sumsqr.pyimport stdio
def sumsquare(b): sum = 0 for k in range(0, len(b)): b[k] *= b[k] sum += b[k] return sum
def main(): a = [3, 5, 7] s = sumsquare(a) stdio.writeln(str(s+a[2]))
if __name__ == "__main__": main()
The echo command "echoes" text to your terminal. What does the following command print to the terminal when executed?
Die echo bevel druk 'n gegewe string na die terminaal. Wat druk die volgende bevel na die terminaal as dit uitgevoer word?
echo "F F" | python dragon.py | python dragon.py | python dragon.py
#dragon.pyimport stdiodef main(): dragon = stdio.readString() nogard = stdio.readString() stdio.write(dragon + "L" + nogard) stdio.write(" ") stdio.write(dragon + "R" + nogard) stdio.writeln()if __name__ == "__main__": main()
What does the following code print to the terminal when executed?
Wat druk die volgende kode na die teminaal wanneer dit uitgevoer word?
#printobjects.pyimport stdiodef main(): GEOMETRIC_OBJECT = ["triangle", "square", "circle", "diamond", "pentagon"] COLOR = ["red", "blue","green", "yellow"] for col in COLOR: for gobj in GEOMETRIC_OBJECT: stdio.writeln(col+" "+gobj)if __name__ == "__main__": main()
What does the following code print to the terminal when executed?
Wat druk die volgende kode na die teminaal wanneer dit uitgevoer word?
#matrixmultiply.pyimport stdioimport stdarraydef main(): A = [[5, 4, 3],[2, 1, 7],[8, 4, 9]] B = [[1, 2, 2],[5, 5, 1],[3, 4, 4]] N = len(A) C = stdarray.create2D(N,N,0) for i in range(0, N): for j in range(0,N): for k in range(0,N): C[i][j] += A[i][k]*B[k][j] stdio.writeln(str(C[1][1]))if __name__ == "__main__": main()
What does the following program print to the terminal when executed?
Wat druk die volgende program na die terminaal as dit uitgevoer word?
import stdioimport stdarraydef findValue(M,x): j = 0 i = 0 while (i < len(M)): while (j < len(M[0])): if M[i][j] == x: return True i += 1 j += 1 return Falsedef main(): M = stdarray.create2D(400,3,0) M[300][2] = 5 stdio.writeln(findValue(M,5))if __name__ == "__main__": main()
Is the following statement true or false:
Is die volgende stelling waar of onwaar:
The following function definition contains no syntax errors.
Die volgende funksie definisie bevat geen sintaks foute nie.
def harmonic (n): sum = 0.0 for i in range(1,n+1): sum += 1.0/i return sum
An avatar is a representation of a human on the internet. It could be an icon, or an image, or even an animated 3D model. In this exercise we are going to generate an avatar based on your name.
Write a program avatars.py that accepts one command-line arguments: a name. Write a function called get_hashcode() that converts this name (a String s of length n) to an integer hash using the following algorithm:
Hint: use the function ord() to get the unicode integer representation of the a character.
Next, in your main code, calculate five indices for each of the five body parts of the avatar:
hash % 15 is an integer 0..14 that denotes the body
(hash / 15) % 10 is an integer 0..9 that denotes the fur
(hash / 150) % 15 is an integer 0..14 that denotes the eyes
(hash / 2250) % 10 is an integer 0..9 that denotes the mouth
(hash / 22500) % 20 is an integer 0..19 that denotes an extra accessory
For a body part with index 5, the corresponding image is called body_5.png (or fur_5.png or eyes_5.png or mouth_5.png or extra_5.png). Your program must use stddraw to create a 256x256 pixel window and load and display the body parts in the order given above.
IMPORTANT: You need to add the following line to the top of your file for you to be able to use Picture:
from picture import Picture
Hint: to draw, say for example the body_5.png, at the center of the canvas use:
body_pic = Picture('body_5.png')
stddraw.picture(body_pic, 0.5, 0.5)
Download avatars.zip for the full collection of body parts.
Your program should work as follows:
$ python avatar.py Garfield
$ python avatar.py Odie
$ python avatar.py "Jon Arbuckle"
The combination of art and mathematics has a long history stretching back to the Ancient Greeks. The introduction of the computer obviously opened up many new avenues, and was used for creating art since the 1950s.
Nowadays, *Computer art* is a substantial field. Here are a few examples:
https://joshuadavis.com/
http://www.verostko.com/
http://www.museumofcomputerart.org/
http://aaronshome.com/aaron/index.html Probably the best-known "computer" artist.
Write a program randomtiling.py that fills a rectangular area with randomly-placed, non-overlapping circles. The circles will start large and will grow smaller and smaller. The program must accept two command-line arguments: c is a real number that controls how quickly the circles grow smaller, and N is an integer that specifies the number of circles to draw.
The program must record all the circles (their center point and radius) that it draws, so that it can check if a circle overlaps with an already drawn circle. (Store them in three arrays for the x-coordinate, y-coordinate, and radius.) Write a method intersect(x, y, r) to calculate if a new circle at (x, y) with radius r overlaps any previous circle. If it does, the method should return True, and False otherwise. Here is a major hint of how to implement this routine. Think about the relationship between d and r and r1 as the circles get closer and closer, eventually overlapping.
The following was produced with c = 0.5 and N = 100.
Below is the pseudo-code (= "code outline") to draw the circles.
Define a constant MAX_TRIES (about 50) that limits the numbers of attempts to place a new circle. If we cannot place a circle after MAX_TRIES tries, we move on to the next, smaller circle. Also, define contstants WIDTH and HEIGHT to be 500 to be the size of the canvas and scale x and y accordingly.
As this code mentions, the initial area is calculated using a formula related to the Riemann zero function. For more information on the Rieman zeta function see page 218 of the textbook or this Wikipedia article. This is an extremely important function in Mathematics, and it tends to pop up in a lot of places. Here is the code for the calculation of the area of the initial circle, given the total area of the square (area).
By playing around with the colour of new objects, you should be able to produce pictures like the following:
You may also want to change your program to draw squares instead of circles:
Or decorate the circles in another way:
An avatar is a representation of a human on the internet. It could be an icon, or an image, or even an animated 3D model. In this exercise we are going to generate an avatar based on your name.
Write a program avatars.py that accepts one command-line arguments: a name. Write a function called get_hashcode() that converts this name (a String s of length n) to an integer hash using the following algorithm:
Hint: use the function ord() to get the unicode integer representation of the a character.
Next, in your main code, calculate five indices for each of the five body parts of the avatar:
hash % 15 is an integer 0..14 that denotes the body
(hash / 15) % 10 is an integer 0..9 that denotes the fur
(hash / 150) % 15 is an integer 0..14 that denotes the eyes
(hash / 2250) % 10 is an integer 0..9 that denotes the mouth
(hash / 22500) % 20 is an integer 0..19 that denotes an extra accessory
For a body part with index 5, the corresponding image is called body_5.png (or fur_5.png or eyes_5.png or mouth_5.png or extra_5.png). Your program must use stddraw to create a 256x256 pixel window and load and display the body parts in the order given above.
IMPORTANT: You need to add the following line to the top of your file for you to be able to use Picture:
from picture import Picture
Hint: to draw, say for example the body_5.png, at the center of the canvas use:
body_pic = Picture('body_5.png')
stddraw.picture(body_pic, 0.5, 0.5)
Download avatars.zip for the full collection of body parts.
Your program should work as follows:
$ python avatar.py Garfield
$ python avatar.py Odie
$ python avatar.py "Jon Arbuckle"