Notes from Python Programming for scientists.
See also: https://www.programiz.com/python-programming/examples
One can run python programs either at http://trinket.io, or Python IDLE (Gui based Integrated Development Environment), or Windows command prompt (PATH should be set to C:\Python27 or C:\Python34 for this). Note that programs using file i/o cannot be run in trinket.io (on-line IDE).
Python Programming – Simple Examples
# First python program - hello.py print("Hello World ...")
# Trailing comma i = 256*256 print 'The value of i is', i
# Fibonacci series: # the sum of two elements defines the next a, b = 0, 1 while b < 200: print b, a, b = b, a+b
1 2 3 4 5 6 | # Program to add two numbersnum1 = 2.5 num2 = 4.7 sum = float(num1) + float(num2) # Print the sum print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) |
1 2 3 4 5 | # <strong>Program to calculate the square root</strong># Get input from the user num = float(input('Enter a number: ')) sqroot = num ** 0.5 print('The square root of %0.3f is %0.3f'%(num, sqroot)) |
1 2 3 4 5 6 7 8 9 10 | # Python Program to find the area of a triangle# Get sides as inputs from the user a = float(input('Enter first side: ')) b = float(input('Enter second side: ')) c = float(input('Enter third side: ')) # Calculate semi-perimeter s = (a + b + c) / 2 # Calculate area area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 print('The area of the triangle is %0.2f' %area) |
1 2 3 4 | #Program to generate a random number between 0 and 9# Import the random module import random print(random.randint(0,9)) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Python program to check if the input number is prime or not# A prime number is a positive integer greater than 1 which # ... has no other factors except 1 and the number itself # Get input from the user num = int(input("Enter a number: ")) # Prime numbers are greater than 1 if num > 1: # Check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number") # If input number is less than or equal to 1, it is not prime else: print(num,"is not a prime number") |
1 2 3 4 5 6 7 8 9 10 | # Python program (in trinket) Draw_Squares.pyfrom turtle import * def square (length): for i in range (4): fd(length) rt(90) square(100) square(75) square(50) |
1 2 3 4 5 6 7 8 9 | # Python program (in trinket) Draw_a_Hex.pyfrom turtle import * penup() goto(0,0) pendown() for i in range(6): fd(50) rt(60) done() |
1 2 3 4 5 6 7 8 9 10 11 12 | # Python program (in trinket) Draw_a_Star.pyfrom turtle import * # http://www.dreamincode.net/forums/topic/360410-python-3-turtle-drawing-a-star/ # i = 0 # while i <= 5: for i in range(5): # this actually goes 5 times, not 6 fd(100) # forward 100 pixels lt(180) # point backwards lt(36) # then 36 degrees left # rt(144) # Just this is enough # i+=1 done() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | # Python program (in trinket) Four_squares.pyimport turtle scene = turtle.Screen() def square(colr, length): a.color(colr) a.begin_fill() for i in range(4): a.fd(length) a.rt(90) a.end_fill() def square2(colr, side): a.color(colr) a.begin_fill() a.fd(side) a.lt(90) a.fd(side) a.lt(90) a.fd(side) a.lt(90) a.fd(side) a.end_fill() a = turtle.Turtle() a.penup() a.goto(-25,-25) a.pendown() square("red",150) a.penup() a.lt(180) a.fd(20) a.rt(90) a.pendown() square("blue",130) a.penup() a.lt(180) a.fd(20) a.rt(90) a.pendown() square("green",110) a.penup() a.lt(180) a.fd(20) a.rt(90) a.pendown() square("yellow",90) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # Python program (in trinket) Three_Circles.pyimport turtle def draw_circle(turtle, color, size, x, y): turtle.penup() turtle.color(color) turtle.fillcolor(color) turtle.goto(x,y) turtle.pendown() turtle.begin_fill() turtle.circle(size) turtle.end_fill() tommy = turtle.Turtle() tommy.shape("turtle") tommy.speed(500) draw_circle(tommy, "green", 50, 25, 0) draw_circle(tommy, "blue", 50, 0, 0) draw_circle(tommy, "yellow", 50, -25, 0) tommy.penup() tommy.goto(-70,-50) tommy.color("black") tommy.write("Let's Learn Python!", None, None, "16pt bold") tommy.goto(0,-80) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # Python program (in trinket) Bird_Box_Remix.pyimport turtle wn = turtle.Screen() bird = turtle.Turtle() bird.speed(10) bird.pensize(4) bird.color("OliveDrab") bird.setpos(-50, 0) bird.fillcolor("Olive") bird.begin_fill() for i in [0, 1, 2]: bird.forward(175) bird.left(120) bird.end_fill() bird.forward(25) bird.right(90) bird.fillcolor("PeachPuff") bird.begin_fill() for i in [0, 1, 2]: bird.fd(125) bird.left(90) bird.end_fill() bird.penup() bird.fd(62.5) bird.left(90) bird.forward(25) bird.right(90) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Python program (in trinket) Draw_Sun1.pyfrom turtle import * penup() goto (0,0) pendown() speed(30) pencolor("red") begin_fill() # fillcolor("yellow") fillcolor("lightgreen") rt(10) fd (150) for i in range(36): lt(170) fd(150) end_fill() hideturtle() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # Python program (in trinket) Georgias_Spirals.py# An awesome design by Georgia, age 13, who lives in the UK. import turtle import math import random wn = turtle.Screen() wn.bgcolor('black') Albert = turtle.Turtle() Albert.speed(0) Albert.color('white') rotate=int(360) def drawCircles(t,size): for i in range(10): t.circle(size) size=size-4 def drawSpecial(t,size,repeat): for i in range (repeat): drawCircles(t,size) t.right(360/repeat) drawSpecial(Albert,100,10) Steve = turtle.Turtle() Steve.speed(0) Steve.color('yellow') rotate=int(90) def drawCircles(t,size): for i in range(4): t.circle(size) size=size-10 def drawSpecial(t,size,repeat): for i in range (repeat): drawCircles(t,size) t.right(360/repeat) drawSpecial(Steve,100,10) Barry = turtle.Turtle() Barry.speed(0) Barry.color('blue') rotate=int(80) def drawCircles(t,size): for i in range(4): t.circle(size) size=size-5 def drawSpecial(t,size,repeat): for i in range (repeat): drawCircles(t,size) t.right(360/repeat) drawSpecial(Barry,100,10) Terry = turtle.Turtle() Terry.speed(0) Terry.color('orange') rotate=int(90) def drawCircles(t,size): for i in range(4): t.circle(size) size=size-19 def drawSpecial(t,size,repeat): for i in range (repeat): drawCircles(t,size) t.right(360/repeat) drawSpecial(Terry,100,10) Will = turtle.Turtle() Will.speed(0) Will.color('pink') rotate=int(90) def drawCircles(t,size): for i in range(4): t.circle(size) size=size-20 def drawSpecial(t,size,repeat): for i in range (repeat): drawCircles(t,size) t.right(360/repeat) drawSpecial(Will,100,10) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # Python program (in trinket) Turtle_Sample2A.pyimport turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.goto(30,-150) tina.pendown() tina.circle(130) tina.penup() tina.goto(0,0) tina.pendown() tina.circle(20) tina.circle(10) tina.penup() tina.forward(60) tina.right(45) tina.pendown() tina.circle(30) tina.circle(10) tina.penup() tina.right(90) tina.forward(90) tina.pendown() tina.circle(40) tina.penup() tina.goto(25,-25) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | # Python program (in trinket) Turtle_Sample2B.pyimport turtle tina = turtle.Turtle() tina.shape('turtle') tina.penup() tina.begin_fill() tina.color('green') tina.goto(30,-150) tina.pendown() tina.circle(130) tina.penup() tina.end_fill() tina.color('white') tina.goto(0,0) tina.begin_fill() tina.pendown() tina.circle(20) tina.penup() tina.end_fill() tina.begin_fill() tina.color('black') tina.pendown() tina.circle(10) tina.penup() tina.end_fill() tina.forward(60) tina.right(45) tina.begin_fill() tina.color('white') tina.pendown() tina.circle(30) tina.penup() tina.end_fill() tina.begin_fill() tina.color('black') tina.pendown() tina.circle(10) tina.penup() tina.end_fill() tina.right(90) tina.forward(90) tina.begin_fill() tina.color('maroon') tina.pendown() tina.circle(40) tina.penup() tina.end_fill() tina.hideturtle() # tina.goto(25,-25) # tina.goto(50,-50) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | # Python program (in trinket) Smiley_face.py# Draw a Smiley face with customizable options using turtle # Purpose: Using turtle, write a complete python program that draws a nice smiley face. import turtle #import turtle so we can use it wn = turtle.Screen() #set the screen to wn bob = turtle.Turtle() #and set bob as our turtle #functions to allow users to customize the face def main(color, sp, hide): #the Main function wn.bgcolor(color) #sets the background color bob.speed(sp) #sets bob's speed fastest = 0, fast = 10, normal = 6, slow = 3, slowest = 1 if hide == True: #if hide = true hide our turtle bob.hideturtle() #endif def points(x,y): #Function to bring pen up and down while going to a point bob.penup() #brings pen up bob.goto(x,y) #makes turtle go to set point bob.pendown() #puts pen down def circles(color, size, angle): #Function to create circles and colors them bob.fillcolor(color) #set fill color bob.begin_fill() #start filling if angle == 360: #if the angle is a full circle bob.circle(size) #make circle with set size else: bob.circle(size, angle) #create circle with set size and angle bob.end_fill() #finish filling # Start Main Code Ending the Functions main('black', 3, False) points(0,-225) circles('white',250,360) points(-70,70) circles('blue',50,360) # points(0,-180) # circles('white',180,360) # points(-70,70) # circles('blue',50,360) points(70,70) circles('blue',50,360) bob.right(70) points(-80,-50) circles('red',100,135) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | # Python program (in trinket) My_House1.pyimport turtle scene = turtle.Screen() scene.bgcolor("SkyBlue") evan = turtle.Turtle() evan.shape("turtle") evan.fillcolor("Red") # Draw the base of the house in Red evan.penup() evan.goto(-100,0) evan.fillcolor("Red") evan.pendown() evan.begin_fill() evan.goto(-100,50) evan.goto(100,50) evan.goto(100,0) evan.goto(-100,0) evan.end_fill() # Raise the roof in Brown color evan.penup() evan.goto(-100,50) evan.fillcolor("Brown") evan.pendown() evan.begin_fill() evan.goto(0,100) evan.goto(100,50) evan.goto(-100,50) evan.end_fill() # Add the Orange door evan.penup() evan.goto(-40,0) evan.fillcolor("Orange") evan.pendown() evan.begin_fill() evan.goto(-40,30) evan.goto(-20,30) evan.goto(-20,0) evan.goto(-40,0) evan.end_fill() # Draw the window evan.penup() evan.goto(20,20) evan.fillcolor("White") evan.pendown() evan.begin_fill() evan.goto(20,40) evan.goto(50,40) evan.goto(50,20) evan.goto(20,20) evan.end_fill() # Hide the turtle evan.hideturtle() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | # Python program (in trinket) US_Flag1.pyfrom turtle import * def line (col, len): speed(15) color(col) fd(len) penup() bk(len) rt(90) pendown() fd(5) lt(90) def dotted (col, num): color(col) dot() for i in range(num): fd(5) dot() goto(0,0) goto(0,-100) goto(150,-100) goto(150,0) goto(60,0) for i in range(10): line("red", 90) lt(90) fd(5) penup() goto(0,-50) rt(90) pendown() for i in range(11): line("red", 150) goto(0,0) for j in range(1,11): dotted("blue", 12) penup() goto(0,-j*5) penup() goto(0,-50) rt(90) pendown() color("red") fd(150) # penup() # goto(60,-40) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | # Python program (in trinket) US_Flag2.pyfrom turtle import * speed(6) for i in range(4): fd(150 + i%2*-40) rt(90) for i in range(4): fd(60) rt(90) rt(90) fd(250) goto(0,-60) pendown() rt(-90) fd(150) for i in range(13): color("red") penup() goto(60,-5*i) pendown() fd(90) for i in range(10): penup() goto(0,-60+i*-5) pendown() fd(150) color("red") begin_fill() goto(0,-60+i*-5) end_fill() goto(0,0) dot_distance = 5 width = 13 height = 13 penup() color("blue") for y in range(height): for i in range(width): dot() forward(dot_distance) backward(dot_distance*width) rt(90) forward(dot_distance) left(90) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | # Python program (in trinket) Graphing_Data.py# # Paste the data you wish to graph in tab-delimited rows in the format: # # xdata <tab> ydata # # In this example the xdata is time (s) and y data is y position (cm) # print("This is a graph of y-position vs. time for a falling cupcake cup by Doug Brown and his students.") print('http://www.cabrillo.edu/~dbrown/tracker/air_resistance.pdf') print('') print('Code is based on an example by Steve Spicklemire at:') print('https://github.com/sspickle/sci-comp-notebooks/blob/master/P01-Euler-Intro.ipynb') data = """ 0.000000000E0 -2.688162330E0 3.336670003E-2 -4.301059729E0 6.673340007E-2 -5.376324661E0 1.001001001E-1 -6.989222059E0 1.334668001E-1 -1.129028179E1 1.668335002E-1 -1.451607658E1 2.002002002E-1 -2.043003371E1 2.335669002E-1 -2.526872591E1 2.669336003E-1 -3.118268303E1 3.003003003E-1 -3.870953756E1 3.336670003E-1 -4.623639208E1 3.670337004E-1 -5.430087907E1 4.004004004E-1 -6.236536606E1 4.337671004E-1 -7.150511799E1 4.671338005E-1 -8.010723744E1 5.005005005E-1 -8.924698937E1 5.338672005E-1 -9.892437376E1 5.672339006E-1 -1.080641257E2 6.006006006E-1 -1.177415101E2 6.339673006E-1 -1.274188945E2 6.673340007E-1 -1.370962788E2 7.007007007E-1 -1.467736632E2 7.340674007E-1 -1.575263126E2 7.674341008E-1 -1.672036969E2 8.008008008E-1 -1.768810813E2 8.341675008E-1 -1.865584657E2 8.675342009E-1 -1.973111150E2 9.009009009E-1 -2.075261319E2 9.342676009E-1 -2.182787812E2 9.676343010E-1 -2.284937981E2 """.split('\n') # split this string on the "newline" character. #print len(data) # # The data is stored in a single string. Now, split the data and store # each column in a list. Convert the data from a string to a float. # tlist = [] ylist = [] for s in data: if s: t,y = s.split() # split the string in two t=float(t) # convert time to float y=float(y)/100.0 # convert y-position (cm) to float in meters tlist.append(t) # append to the list for time data ylist.append(y) # append to the list for y-position data #print "tlist=",tlist #print "ylist=",ylist import matplotlib.pyplot as plt plt.title('y-position vs. time for falling cupcake paper') plt.xlabel('t (s)') plt.ylabel('y (m)') plt.plot(tlist,ylist,'m.') plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | # Python program (in trinket) Graphing_data_with_Best_fit_fn.py#import useful libraries import matplotlib.pyplot as plt from numpy import * print('This Trinket shows how to graph data and a best-fit function.') # # Paste the data you wish to graph in tab-delimited rows in the format: # # xdata <tab> ydata # # In this example, the x data is diameter (m) and y data is # terminal speed (m/s). # datalist = """ 0 0 0.00238125 0.00674 0.003175 0.0113 0.00396875 0.0179 0.0047625 0.0242 0.00635 0.0391 """.split('\n') # # Take the list of strings defined above and store it as numbers in lists. # Dlist = [] #Diameter D2list = [] #Diameter squared vlist = [] #terminal speed for s in datalist: if s: D,v = s.split() # split the string into two strings D=float(D) # convert diameter string to float v=float(v) # convert speed string to float Dlist.append(D) # store diameter in a list D2list.append(D*D) # store diameter squared in a list vlist.append(v) # store terminal speed in a list # # Determine the best-fit curve using other software like Logger Pro or # Gnuplot. Define variables for the best-fit function. # Use the array() function to convert the xdata list to an array. # Calculate the theoretical values for "y" to plot on the yaxis. m=1100 #slope b=0 #intercept vtheor=m*array(D2list)+b #y=mx*b is the best-fit function # # Graph data and the best-fit curve. # plt.title('terminal speed vs diameter squared of a steel ball') plt.xlabel('D^2 (m^2)') plt.ylabel('v_term (m/s)') plt.plot(D2list,vlist,'co', D2list, vtheor, 'm-') plt.show() #import useful libraries import matplotlib.pyplot as plt from numpy import * print('This Trinket shows how to graph data and a best-fit function.') # # Paste the data you wish to graph in tab-delimited rows in the format: # # xdata <tab> ydata # # In this example, the x data is diameter (m) and y data is # terminal speed (m/s). # datalist = """ 0 0 0.00238125 0.00674 0.003175 0.0113 0.00396875 0.0179 0.0047625 0.0242 0.00635 0.0391 """.split('\n') # # Take the list of strings defined above and store it as numbers in lists. # Dlist = [] #Diameter D2list = [] #Diameter squared vlist = [] #terminal speed for s in datalist: if s: D,v = s.split() # split the string into two strings D=float(D) # convert diameter string to float v=float(v) # convert speed string to float Dlist.append(D) # store diameter in a list D2list.append(D*D) # store diameter squared in a list vlist.append(v) # store terminal speed in a list # # Determine the best-fit curve using other software like Logger Pro or # Gnuplot. Define variables for the best-fit function. # Use the array() function to convert the xdata list to an array. # Calculate the theoretical values for "y" to plot on the yaxis. m=1100 #slope b=0 #intercept vtheor=m*array(D2list)+b #y=mx*b is the best-fit function # # Graph data and the best-fit curve. # plt.title('terminal speed vs diameter squared of a steel ball') plt.xlabel('D^2 (m^2)') plt.ylabel('v_term (m/s)') plt.plot(D2list,vlist,'co', D2list, vtheor, 'm-') plt.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # Python program Google_List1.pydef match_ends(words): count = 0 for word in words: if len(word) >= 2 and word[0] == word[-1]: count = count + 1 return count def front_x(words): x_list = [] other_list = [] for w in words: if w.startswith('x'): x_list.append(w) else: other_list.append(w) return sorted(x_list) + sorted(other_list) def last(a): return a[-1] def sort_last(tuples): return sorted(tuples, key=last) # A. match_ends # Given a list of strings, return the count of the number of # strings where the string length is 2 or more and the first # and last chars of the string are the same. print 'match_ends' print match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']) print match_ends(['', 'x', 'xy', 'xyx', 'xx']) print match_ends(['aaa', 'be', 'abc', 'hello']) print # B. front_x # Given a list of strings, return a list with the strings # in sorted order, except group all the strings that begin with 'x' first. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] print 'front_x' print front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']) print front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']) print front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']) print # C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] print 'sort_last' print sort_last([(1, 3), (3, 2), (2, 1)]) print sort_last([(2, 3), (1, 2), (3, 1)]) print sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # Python program Google_List2.pydef remove_adjacent(nums): result = [] for num in nums: if len(result) == 0 or num != result[-1]: result.append(num) return result def linear_merge(list1, list2): result = [] # Look at the two lists so long as both are non-empty. # Take whichever element [0] is smaller. while len(list1) and len(list2): if list1[0] < list2[0]: result.append(list1.pop(0)) else: result.append(list2.pop(0)) # Now tack on what's left result.extend(list1) result.extend(list2) return result # D. Given a list of numbers, return a list where # all adjacent == elements have been reduced to a single element, # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or # modify the passed in list. print 'remove_adjacent' print remove_adjacent([1, 2, 2, 3]) print remove_adjacent([2, 2, 3, 3, 3]) print remove_adjacent([]) # E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. print print 'linear_merge' print linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']) print linear_merge(['aa', 'xx'], ['bb', 'cc', 'zz']) print linear_merge(['aa', 'aa'], ['aa', 'bb', 'bb']) |