Skip to content

Instantly share code, notes, and snippets.

View dazcona's full-sized avatar
🤹‍♂️
Juggling with Data

David Azcona dazcona

🤹‍♂️
Juggling with Data
View GitHub Profile
@dazcona
dazcona / copy-n-times.py
Created November 7, 2018 17:08
Output n copies of s on a single line being s and n command line arguments
# Command-line arguments are always strings.
# If we know that an argument is actually an integer, then we have to do the conversion ourselves.
# The first command-line argument is a string s, and the second command-line argument is an integer n.
# Output n copies of s on a single line.
import sys
s = sys.argv[1] # s is a string
n = int(sys.argv[2]) # n is an integer
@dazcona
dazcona / print-arguments.py
Created November 7, 2018 17:06
Print out each command-line argument, together with its index.
import sys
i = 0
while i < len(sys.argv):
print i, sys.argv[i]
i = i + 1
@dazcona
dazcona / arguments.py
Created November 7, 2018 17:05
Command-Line Arguments
import sys
print sys.argv[0] # the name of the current script
print sys.argv[1] # the first command-line argument
print sys.argv[2] # the second command-line argument
print sys.argv[3] # the third command-line argument
# ... and so on
@dazcona
dazcona / slice-list.py
Created November 7, 2018 17:04
Lists Slices
a = [6, 3, 7, 8, 3, 7, 8, 2]
a[1:] # [3, 7, 8, 3, 7, 8, 2]
a[2:4] # [7, 8]
a[:] # [6, 3, 7, 8, 3, 7, 8, 2]
@dazcona
dazcona / reverse-list.py
Created November 7, 2018 17:03
Reverse a List In Place
i = 0
while i < len(a)/2:
tmp = a[i]
a[i] = a[len(a) - i - 1]
a[len(a) - i - 1] = tmp
i = i + 1
@dazcona
dazcona / reverse-lines.py
Created November 7, 2018 17:02
Reverse Lines of Standard Input
lines = []
line = raw_input()
while line != "end":
lines.append(line)
line = raw_input()
i = 0
while i < len(lines):
print lines[len(lines) - i - 1]
@dazcona
dazcona / pop.py
Created November 7, 2018 17:02
Pop an element from a list
a = [1, 2, 3, 4]
a.pop() # 4
# a is now [1, 2, 3]
a.pop() # 3
# a is now [1, 2]
@dazcona
dazcona / copy-to-list.py
Created November 7, 2018 16:59
Read a sequence of lines from standard input into a list a. Stop when a line containing only the text "end" is encountered.
a = []
line = raw_input()
while line != "end":
a.append(line)
line = raw_input()
@dazcona
dazcona / append.py
Created November 7, 2018 16:58
Append to list
a = [1, 2]
a.append(3)
print a # [1, 2, 3]
@dazcona
dazcona / maximum.py
Created November 7, 2018 16:58
Maximum of a list, assuming it will have at least one element
# a = [ 10, 20, 3, 17, 100 ]
a = [ -10, -20, -3, -17, -100 ]
maximum = a[0]
i = 1
while i < len(a):
if maximum < a[i]:
maximum = a[i]
i = i + 1