SHARE
TWEET

Untitled

a guest Nov 20th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. def all_nums(n):
  3.     for i in range(1, n+1):
  4.         sys.stdout.write("{} ".format(i))
  5.  
  6. def all_nums_squared_horizontal(n):
  7.     for i in range(1, n+1):
  8.         sys.stdout.write("{} ".format(i**2))
  9.  
  10. def all_nums_squared_vertical(n):
  11.     for i in range(1, n+1):
  12.         sys.stdout.write("{}\n".format(i**2))
  13.  
  14. def all_nums_dividing_by_three(n):
  15.     for i in range(1, n+1):
  16.         if i%3==0:
  17.             sys.stdout.write("{} ".format(i))
  18.  
  19. def all_nums_not_dividing_by_three(n):
  20.     for i in range(1, n+1):
  21.         if not i%3==0:
  22.             sys.stdout.write("{} ".format(i))
  23.  
  24. def num_triangle(n):
  25.     for x in range(1, n+1):
  26.         for y in range(1, x+1):
  27.             sys.stdout.write("{} ".format(y))
  28.         sys.stdout.write("\n")
  29.  
  30. def num_triangle_flipped(n):
  31.     for x in range(1, n+1):
  32.         sys.stdout.write("  "*(n-x))
  33.         for y in range(1, x+1):
  34.             sys.stdout.write("{} ".format(y))
  35.         sys.stdout.write("\n")
  36.  
  37. def num_triangle_centered(n):
  38.     for x in range(1, n+1):
  39.         sys.stdout.write(" "*(n-x))
  40.         for y in range(1, x+1):
  41.             sys.stdout.write("{} ".format(y))
  42.         sys.stdout.write("\n")
  43.  
  44. def pascal_triangle(rows):
  45.     ar=[[0, 1, 0]]
  46.     for y in range(1, rows+1):
  47.         ta = [0, 0]
  48.         for x in range(1, len(ar)+1):
  49.             ta.insert(-1, ar[-1][x-1]+ar[-1][x])
  50.         ar.append(ta)
  51.     return ar
  52.  
  53. sys.stdout.write("Print all numbers from 1 to n\n")
  54. all_nums(5)
  55.  
  56. sys.stdout.write("\n\nPrint all numbers from 1 to n squared horizontally\n")
  57. all_nums_squared_horizontal(5)
  58.  
  59. sys.stdout.write("\n\nPrint all numbers from 1 to n squared vertically\n")
  60. all_nums_squared_vertical(5)
  61.  
  62. sys.stdout.write("\n\nPrint all numbers from 1 to n that divide by 3\n")
  63. all_nums_dividing_by_three(30)
  64.  
  65. sys.stdout.write("\n\nPrint all numbers from 1 to n that do not divide by 3\n")
  66. all_nums_not_dividing_by_three(30)
  67.  
  68. sys.stdout.write("\n\nPrint all numbers from 1 to n in a triangle\n")
  69. num_triangle(5)
  70.  
  71. sys.stdout.write("\n\nPrint all numbers from 1 to n in a triangle flipped\n")
  72. num_triangle_flipped(5)
  73.  
  74. sys.stdout.write("\n\nPrint all numbers from 1 to n in a triangle centered\n")
  75. num_triangle_centered(5)
  76.  
  77. sys.stdout.write("\n\nWrite a pascal triangle with n rows\n")
  78. p=pascal_triangle(10)
  79. for x in range(1, len(p)):
  80.     sys.stdout.write(" "*(len(p)-x))
  81.     for y in p[x]:
  82.         sys.stdout.write("{} ".format(y))
  83.     sys.stdout.write("\n")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top