SlideShare a Scribd company logo
For any help regarding Python Homework Help
Visit :- https://www.pythonhomeworkhelp.com/ ,
Email :- support@pythonhomeworkhelp.com or
Call us at :- +1 678 648 4277
Python Homework Help
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.
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.
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.
Python Homework Help
Problem
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:
Using this list, you can go through each word and convert it to Pig-Latin. Also, to get a word except for the
first letter, you can use word[1:].
Hints: It will make your life much easier — and your code much better — if you separate tasks into
functions, e.g. have a function that converts one word to Pig-Latin rather than putting it into your main
program code.
Python Homework Help
# collision.py
# Example solution for Lab 6, problem 1
#
# Aseem Kishore
#
# 6.189 - Intro to Python
# IAP 2008 - Class 4
# 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]
Python Homework Help
Solution
# 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:",
Python Homework Help
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!"
Python Homework Help
# piglatin.py
# Example solution for Lab 6, problem 2
#
# Aseem Kishore
#
# 6.189 - Intro to Python
# IAP 2008 - Class 4
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
Python Homework Help
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)
Python Homework Help

More Related Content

PPTX
Python Programming Assignments: Tuples and String
PDF
Python-Cheat-Sheet.pdf
PPT
Phyton Learning extracts
PPTX
Python Homework Help
PPT
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
PPTX
Mastering Python lesson3b_for_loops
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
DOCX
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
Python Programming Assignments: Tuples and String
Python-Cheat-Sheet.pdf
Phyton Learning extracts
Python Homework Help
PPT3-CONDITIONAL STATEMENT LOOPS DICTIONARY FUNCTIONS.ppt
Mastering Python lesson3b_for_loops
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx

Similar to Python Homework Help (20)

DOCX
What is the general format for a Try-Catch block Assume that amt l .docx
PPTX
Code Like Pythonista
PPTX
foundation class python week 4- Strings.pptx
PDF
PPTX
Python Homework Help
PPT
Lập trình C
PPTX
Improve Your Edge on Machine Learning - Day 1.pptx
PDF
Notes5
PDF
Class 7a: Functions
PPTX
Python Homework Help
PPTX
ForLoops.pptx
PPTX
loopsnote................, b .pptx
PPTX
powerpoint 2-7.pptx
PDF
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
PDF
Lesson 03 python statement, indentation and comments
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PDF
Real World Haskell: Lecture 2
PPTX
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
What is the general format for a Try-Catch block Assume that amt l .docx
Code Like Pythonista
foundation class python week 4- Strings.pptx
Python Homework Help
Lập trình C
Improve Your Edge on Machine Learning - Day 1.pptx
Notes5
Class 7a: Functions
Python Homework Help
ForLoops.pptx
loopsnote................, b .pptx
powerpoint 2-7.pptx
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Lesson 03 python statement, indentation and comments
CHAPTER 01 FUNCTION in python class 12th.pptx
Real World Haskell: Lecture 2
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Ad

More from Python Homework Help (20)

PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Complete my Python Homework
PPTX
Introduction to Python Dictionary.pptx
PPTX
Basic Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Introduction to Python Programming.pptx
PPTX
Python Homework Help
PPTX
Python Homework Help
PPTX
Python Programming Homework Help.pptx
PPTX
Quality Python Homework Help
PPTX
Perfect Python Homework Help
Python Homework Help
Python Homework Help
Python Homework Help
Python Homework Help
Python Homework Help
Complete my Python Homework
Introduction to Python Dictionary.pptx
Basic Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Introduction to Python Programming.pptx
Python Homework Help
Python Homework Help
Python Programming Homework Help.pptx
Quality Python Homework Help
Perfect Python Homework Help
Ad

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Structure & Organelles in detailed.
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Supply Chain Operations Speaking Notes -ICLT Program
A systematic review of self-coping strategies used by university students to ...
human mycosis Human fungal infections are called human mycosis..pptx
GDM (1) (1).pptx small presentation for students
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial disease of the cardiovascular and lymphatic systems
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra

Python Homework Help

  • 1. For any help regarding Python Homework Help Visit :- https://www.pythonhomeworkhelp.com/ , Email :- support@pythonhomeworkhelp.com or Call us at :- +1 678 648 4277 Python Homework Help
  • 2. 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. 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. 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. Python Homework Help Problem
  • 3. 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: Using this list, you can go through each word and convert it to Pig-Latin. Also, to get a word except for the first letter, you can use word[1:]. Hints: It will make your life much easier — and your code much better — if you separate tasks into functions, e.g. have a function that converts one word to Pig-Latin rather than putting it into your main program code. Python Homework Help
  • 4. # collision.py # Example solution for Lab 6, problem 1 # # Aseem Kishore # # 6.189 - Intro to Python # IAP 2008 - Class 4 # 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] Python Homework Help Solution
  • 5. # 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:", Python Homework Help
  • 6. 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!" Python Homework Help
  • 7. # piglatin.py # Example solution for Lab 6, problem 2 # # Aseem Kishore # # 6.189 - Intro to Python # IAP 2008 - Class 4 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 Python Homework Help
  • 8. 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) Python Homework Help