# Introduction to arrays
array = [] # defining array which is rather called list in python
print "You are to enter 5 integers.\n"
# Input the 5 integers into array "array"
for i in range(5) : # equivalent to for i in range(0,5,1)
print "Enter integer #%d: "%(i+1),
val = int(raw_input())
array.insert(i,val) # insert function is used to add elements to list :
# insert(i,val) where i is the index and val is the value to be inserted
print "\nThank you.\n"
# Print out the integers stored in array "array"
j = 1 # used as a counter
for i in array: # shorthand way of scanning through the list; using "in"
print "Integer #%d = %d."%(j,i)
j += 1
# Initializing a global array
array = [ 4, 6, 5, 7, 2 ]
def main() : # just a user-defined function ; not equivalent to the built-in main() function in C
""" prints the integers stored in array "array" """
j = 1 # counter
for i in array :
print "Integer #%d = %d."%(j,i)
j += 1
main()
# The number of array elements actually used may vary
# Read in the size of the array
print "How many integers will you enter?",
array_size = 6
print array_size
# Read in the integers
print "\nPlease enter %d integers:"%array_size
array = [ 12, 3, -5, 21, 0, 899 ] # Lists dynamically allocate memory
for i in range(array_size) :
print array[i],
# Print out the integers
print("\n")
for i in range(array_size) :
print "Integer #%d = %d."%(i+1,array[i])
# The trailer method
# Macro definition
MAX_ARRAY_SIZE = 20
TRAILER = -9999
# Read in the integers
print "Please enter up to %d integers, using %d as a trailer:"%(MAX_ARRAY_SIZE,TRAILER)
array = [ 3, 75, -34, 6, -1, 2, 41, 0, 90, -9999]
for array_size in range(MAX_ARRAY_SIZE) :
print array[array_size],
if array[array_size] == -9999 :
break
# Print out the integers
print "\n\nYou have entered %d integers"%array_size
for i in range(array_size) :
print "Integer #%d = %d."%(i+1,array[i])
# Caculating the average grade for a class
# Macro definition
MAX_GRADES = 20 # The class may have up to 20 students
TRAILER = -9999 # -9999 marks the end of the grade list
# Read in the grades
print "Please enter up to %d grades, using %d as a trailer:"%(MAX_GRADES,TRAILER)
grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]
for num_grades in range(MAX_GRADES) :
print grades[num_grades]
if grades[num_grades] == -9999 :
break
print "\nYou have entered %d grades."%num_grades
# Find the average
sum = 0.0
for i in range(num_grades) :
sum += grades[i]
print "\nThe average grade is %f."%(sum/num_grades)
# Determining which grades in a class are above the average
# Macro definition
MAX_GRADES = 20 # The class may have up to 20 students
TRAILER = -9999 # -9999 marks the end of the grade list
# Read in the grades
print "Please enter up to %d grades, using %d as a trailer:"%(MAX_GRADES,TRAILER)
grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]
for num_grades in range(MAX_GRADES) :
print grades[num_grades]
if grades[num_grades] == -9999 :
break
print "\nYou have entered %d grades."%num_grades
# Find the average
sum = 0.0
for i in range(num_grades) :
sum += grades[i]
avg_grade = sum/num_grades
print "\nThe average grade is %f."%avg_grade
# Find who got above average
print "\nThe following grades were above average:"
for i in range(num_grades) :
if grades[i] > avg_grade :
print "%f (student #%d)"%(grades[i],i+1)
# Reversing an array
# Macro definition
ARRAY_SIZE = 10 # The array has 10 elements
# Read in the array
print "Please enter %d integers:"%ARRAY_SIZE
array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]
for i in range(10) :
print array[i],
# Echo the array
print "\n\nThe original array was:"
for i in range(10) :
print array[i],
# Print the array in reverse
print "\n\nThe reversed array is:"
for i in range(ARRAY_SIZE-1,-1,-1) :
print array[i],
import sys
# Reversing a character string
# Macro definition
MAX_STRING_SIZE = 10
# Read in the word
print "Please enter a word of up to %d letters:"%MAX_STRING_SIZE
word = "bingo"
print word
# Echo the word
print "\nThe word is %s."%word
# Find thw word length
string_size = 0
for i in word : # To iterate over the entire string
string_size += 1
# Print the word in reverse
print "\nThe reversed word is ",
for i in range(string_size-1,-1,-1) :
sys.stdout.write("%c"%word[i]) # For printing in the same line without implicit whitespaces
print
# Calculating the minimum and maximum grade in a class
# Macro definition
MAX_GRADES = 20 # The class may have up to 20 students
TRAILER = -9999 # -9999 marks the end of the grade list
# Read in the grades
print "Please enter up to %d grades, using %d as a trailer:"%(MAX_GRADES,TRAILER)
grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]
for num_grades in range(MAX_GRADES) :
print grades[num_grades]
if grades[num_grades] == -9999 :
break
print "\nYou have entered %d grades."%num_grades
# Find the minimum grade
min = grades[0]
for i in range(num_grades) :
if grades[i] < min :
min = grades[i]
print "\nThe lowest grade is %f"%min
# Find the maximum grade
max = grades[0]
for i in range(num_grades) :
if grades[i] > min :
max = grades[i]
print "\nThe lowest grade is %f"%max
# Sorting an array
# Macro definition
MAX_ARRAY_SIZE = 20 # The array may have up to 20 elements
array_size = 10
print "How many numbers?",
print array_size
while array_size < 1 or array_size > MAX_ARRAY_SIZE :
print "How many numbers?",
print array_size
# Read in the array
print "Please enter the integers:"
array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]
for i in range(array_size) :
print array[i],
# Echo the array
print "\n\nThe original array is:"
for i in range(array_size) :
print array[i],
# Sort the array
for i in range(array_size-1) :
for j in range(i+1, array_size) :
if array[i] > array[j] :
temp = array[i]
array[i] = array[j]
array[j] = temp
# Printing the sorted array
print "\n\nThe sorted array is"
for i in range(array_size) :
print array[i],
# Finding the median of an array
# Macro definition
MAX_ARRAY_SIZE = 20 # The array may have up to 20 elements
array_size = 0
array = []
def get_size() :
""" reads the size of the array (until a valid value is entered) """
global array_size
array_size = 10
print "How many numbers?",
print array_size
while array_size < 1 or array_size > MAX_ARRAY_SIZE :
print "How many numbers?",
print array_size
def read_array() :
""" read in the array """
global array
print "Please enter the integers:"
array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3 ]
for i in range(array_size) :
print array[i],
def write_array() :
""" write out the array """
for i in range(array_size) :
print array[i],
def sort_array() :
""" sort the array """
global array
for i in range(array_size-1) :
for j in range(i+1, array_size) :
if array[i] > array[j] :
temp = array[i]
array[i] = array[j]
array[j] = temp
def even(n) :
""" returns non-zero(true) if n is even else returns zero(false) """
return ((n/2)*2 == n)
get_size() # Read in the size of the array
read_array() # Read in the array
print "\n\nThe original array is:"
write_array() # Echo the array
sort_array() # Sort the array
print "\n\nThe sorted array is:"
write_array() # Print the sorted array
print "\n\nThe median is:"
# The equivalent of tertiary conditional operator in python is a if test else b
print "%.1f"%( ((array[ array_size / 2 -1] + array[ array_size / 2 ]) / 2.0 ) if even(array_size) else float(array[ array_size / 2 ]) )
# Tally how many of each number within a range are entered
# Macro definition
RANGE_LOW = 1
RANGE_HIGH = 10 # Tallies numbers between RANGE_LOW and RANGE_HIGH
TRAILER = -1 # TRAILER marks end of data
inputfeed = [ 3, 5, 1, 10, 8, 5, 10, 6, 2, 11, 4, 9, -2, 3, 2, 9, -1 ] # additional array for the sake of providing input
tally = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # empty array
# Input numbers
print "Please enter a number between %d and %d (%d to quit): "%(RANGE_LOW,RANGE_HIGH,TRAILER),
num = inputfeed[0]
print num
i = 1
while num != TRAILER :
if num < RANGE_LOW or num > RANGE_HIGH : # Out of range
print "%d is not between %d and %d."%(num,RANGE_LOW,RANGE_HIGH)
else :
tally[ num - RANGE_LOW] += 1
print "\nEnter another number: ",
num = inputfeed[i]
print num
i += 1 # to iterate over the input array
# Print totals
print "\n------------\n\nYou entered:"
for i in range(RANGE_HIGH - RANGE_LOW + 1) :
print "%2d %2d's"%(tally[i],i + RANGE_LOW)
print "\nThank you."
# Program to report car prices. User enters car make by number and car year.
# Program reports price. All data is fictitious
# 2-D array (4 X 12 ) of prices : row = make , column = year
price_list = [ [ 4775, 4980, 5222, 5305, 5483, 5547, 5596, 5713, 5842, 5903, 6043, 6230 ], # Volkswagen
[ 4853, 5140, 5413, 5590, 5723, 5848, 5762, 5944, 6104, 6255, 6370, 6526 ], # Chevy
[ 5627, 5772, 5973, 6210, 6539, 6720, 6792, 6930, 7054, 7202, 7355, 7562 ], # Cadillac
[ 1576, 1738, 1970, 2151, 2205, 2280, 2442, 2580, 2814, 1953, 3078, 3201 ], # Honda
]
def get_make() :
""" read in and return the number (make) of the desired car """
# Print the choice numbers and names
for i in range(1,5) :
print "%d="%i,
print_make(i)
print ",",
print "0=END"
# Read in the selection by number
print " Which car do you want? ",
make = 2
print make
while make < 0 or make > 4 :
print " Which car do you want? ",
make = 2
print make
return make
def get_year() :
""" read in and return the year of the desired car """
print " What year (1975-1986)?",
year = 1980
print year
while year < 1975 and year > 1986 :
print " What year (1975-1986)?",
year = 1980
print year
return year
def price (car,yr) :
""" Use "car" and "year" to generate an index into "price_list" and return the corresponding entry """
return price_list[ car - 1 ][ yr - 1975]
def print_make(car) :
""" print the make of "car" according to its value """
if car == 1 :
print "Volkswagen",
elif car == 2 :
print "Chevy",
elif car == 3 :
print "Cadillac",
else :
print "Honda",
make = get_make()
while make != 0 : # O indicates end of input
year = get_year()
print "The price of a %d"%year,
print_make(make)
print " is $%d.\n"%price(make,year)
make = 0 # To terminate the loop
print "\nThank you for buying all those cars!"