ComputerNetworkAssig2
34 views
13 slides
Jun 21, 2024
Slide 1 of 13
1
2
3
4
5
6
7
8
9
10
11
12
13
About This Presentation
Explore the intricacies of Python programming with our comprehensive sample assignment on Tuples and Strings. This presentation is designed to provide a deep dive into essential Python concepts, offering detailed explanations and practical examples that are perfect for students and beginners looking...
Explore the intricacies of Python programming with our comprehensive sample assignment on Tuples and Strings. This presentation is designed to provide a deep dive into essential Python concepts, offering detailed explanations and practical examples that are perfect for students and beginners looking to strengthen their understanding of these fundamental data structures.
What’s Inside:
Overview of Tuples and Strings: Understand the core concepts, including the immutability of tuples and versatile string manipulations.
Practical Examples: Step-by-step solutions to real-world problems, illustrating how to effectively use tuples and strings in Python.
Common Pitfalls and Tips: Learn how to avoid common mistakes and optimize your code for better performance.
Hands-on Assignment: A sample assignment designed to test your knowledge and improve your coding skills, complete with thorough solutions and explanations.
Why Download:
Educational Resource: Ideal for students and educators looking for quality educational material on Python.
Assignment Help: A valuable resource for anyone needing help with Python assignments, especially on topics related to tuples and strings.
Expert Insights: Gain insights from programming experts to enhance your learning and coding proficiency.
Get a head start in mastering Python’s tuples and strings. Download now and elevate your programming skills!
Size: 109.16 KB
Language: en
Added: Jun 21, 2024
Slides: 13 pages
Slide Content
Enhancing Problem-Solving Skills through Collision Detection and Pig-Latin Conversion Python Programming Assignments: Tuples and String For any Assignment related queries, Call us at : - +1(315) 557 6473 You can mail us at : - [email protected] or Reach us at : - https://www.programminghomeworkhelp.com/
Problem 1 – Collision detection of balls Many games have complex physics engines, and one major function of these engines is to figure out if two objects are colliding. Weirdly-shaped objects are often approximated as balls. In this problem, we will figure out if two balls are colliding. We will think in 2D to simplify things, though 3D isn’t different conceptually. For calculating collision, we only care about a ball’s position in space and its size. We can store position with its center x-y coordinates, and we can use its radius for size. So a ball is a tuple of (x, y, r). To figure out if two balls are colliding, we need to compute the distance between their centers, then see if this distance is less than the sum of their radii. If so, they are colliding. Write a function that takes two balls and computes if they are colliding. Then call the function with two sets of balls. The first set is (0, 0, 1) and (3, 3, 1); these should not be colliding. The second set is (5, 5, 2) and (2, 8, 3); these should be colliding.
Collision Detection of Balls Objective : Determine if two balls are colliding in a 2D space. Ball Representation : Tuple of (x, y, r) x,yx , yx,y : Center coordinates of the ball. rrr : Radius of the ball. Collision Detection Logic Step 1 : Calculate the distance between the centers of the two balls. Step 2 : Check if this distance is less than the sum of their radii. Formula : Distance = (x2−x1)2+(y2−y1)2\ sqrt {(x2 - x1)^2 + (y2 - y1)^2}(x2−x1)2+(y2−y1)2 Collision Condition : Distance<r1+r2\text{Distance} < r1 + r2Distance<r1+r2 Function Implementation
# collision.py # Problem 1 # # # Imports should usually go at the top of a program instead of in the main code. from math import * # These helper functions let me "abstract away" the syntax of getting a ball's # x- and y- coordinates, or its radius. This makes my code more readable and # also helps prevent bugs where I use x instead of y, etc. def get_x (ball): return ball[0]
def get_y (ball): return ball[1] def get_r (ball): return ball[2] # I got this function from the second day of class. We've been trying to tell # you guys the importance of functions; here's one -- reuse. There are many # applications for finding the distance between two points; detecting collision # is one, so we can reuse the function. This is also why we don't ask for input
# or print our result inside the function. def distance(x1, y1, x2, y2): return sqrt ((x2-x1)**2 + (y2-y1)**2) # Here is my detect collision function. Note that I'm NOT taking six variables # like x1, y1, r1, x2, y2, r2 -- that's the purpose of combining x, y, r into a # tuple , as every ball has an x, y and r. def collision(ball1, ball2): d = distance( get_x (ball1), get_y (ball1), get_x (ball2), get_y (ball2)) sum_of_radii = get_r (ball1) + get_r (ball2) return d < sum_of_radii # My test cases
print "First test case:", a = (0, 0, 1) b = (3, 3, 1) if collision(a, b): print "Oops, we detected a collision!" else: print "Passed!" print "Second test case:", a = (5, 5, 2) b = (2, 8, 3) if collision(a, b): print "Passed!" else: print "Oops, we didn't detect a collision!"
Problem 2 – Pig-Latin Converter Write a program that lets the user enter in some English text, then converts the text to Pig-Latin. To review, Pig-Latin takes the first letter of a word, puts it at the end, and appends “ay”. The only exception is if the first letter is a vowel, in which case we keep it as it is and append “hay” to the end. E.g. “hello” “ ellohay ”, and “image” “ imagehay ” It will be useful to define a list or tuple at the top called VOWELS. This way, you can check if a letter x is a vowel with the expression x in VOWELS. It’s tricky for us to deal with punctuation and numbers with what we know so far, so instead, ask the user to enter only words and spaces. You can convert their input from a string to a list of strings by calling split on the string:
“My name is John Smith”.split (“ ”) [“My”, “name”, “is”, “John”, “ Sm Using this list, you can convert it to Pig-Latin. Also to get a word except for the first letter, you can use word[1:]. Objective : Convert English text to Pig-Latin. Pig-Latin Rules : If a word starts with a consonant: Move the first letter to the end and add "ay". If a word starts with a vowel: Add "hay" to the end. Vowels Definition VOWELS : ('a', 'e', ' i ', 'o', 'u')
# piglatin.py # Solution problem 2 # VOWELS = ('a', 'e', ' i ', 'o', 'u') # Helper function that converts one word into Pig-Latin. Remember, our word is # the function's argument, like 4 is the argument in sqrt (4). We don't need to # know anything about the sentence from which the word came. # # Also, remember that strings are index-able, just like lists and tuples . But, # they are immutable, like tuples . So when we want to append "ay" or "hay" to # the end, we can't use append(). But, we can use the string concatenation (+) # operator to return a new string. # # e.g. we can't do " a".append ("b"), but we can do "a" + "b". def convert_word (word) :
first_letter = word[0] if first_letter in VOWELS: # if word starts with a vowel... return word + "hay" # then keep it as it is and add hay to the end else: return word[1:] + word[0] + "ay" # like the lab mentions, word[1:] # returns the word except word[0] # From this function, it's easy to take a sentence and convert it to Pig-Latin. def convert_sentence (sentence): list_of_words = sentence.split (' ') new_sentence = "" # we'll keep concatenating words to this... for word in list_of_words : new_sentence = new_sentence + convert_word (word) # ...like this new_sentence = new_sentence + " " # but don't forget the space! return new_sentence
# Now, let's write the main program code, to ask the user and convert. print "Type in a sentence, and it'll get converted to Pig-Latin!" print "Please don't use punctuation or numbers." print "Also, we can't handle uppercase/lowercase yet, so lowers only please!" print text = raw_input () # nothing in the parentheses, because there's nothing else # extra to tell the user before he is allowed to type print print convert_sentence (text)
Summary Problem 1 : Implemented collision detection function for two balls using their center coordinates and radii. Problem 2 : Developed a Pig-Latin converter that transforms English sentences by specific linguistic rules. Contact Information Website : programminghomeworkhelp.com Email : [email protected] Phone : +1 (315) 557-6473