python practice

Some code I wrote while practicing python one day. A couple of the problems had built in solutions that I was not aware of so I basically coded the solution the hard way. In a way that’s cool because it shows that I could have written that actual function that’s included in python to create the “shortcut” but it also shows that I did not know the easy way to do it.

amazon_aws_prefix.py

import requests
import json 

r = requests.get("https://ip-ranges.amazonaws.com/ip-ranges.json")
d = r.json() 

ip_prefix_list = d.get("prefixes")

for i in ip_prefix_list:
   ip = i.get("ip_prefix")
   print (ip)

caesar_cipher.py

import string

message = raw_input("\nPlease input your message.\n")
shift = int(raw_input("What is the shift? "))
codec = raw_input("\nPress 1 to encode, anything else will decode\n")

# decode option is simple math - inverse operations
if codec != '1':
    shift = 52 - shift


def caesar(message, shift):
    alphabet = string.ascii_letters
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = string.maketrans(alphabet, shifted_alphabet)
    return message.translate(table)


caesar(message, shift)

if __name__ == '__main__':
    main()

string_reverse_easy.py

import string

print string.ascii_lowercase[::-1]

string_reverse_long.py

import string

my_array = []
array = list(string.ascii_lowercase)

while array:
    my_array.append(array.pop())

reverse_string = "".join(my_array)

print reverse_string

plaindrome_easy.py

seq = raw_input("Enter a word please").lower()
if seq == seq[::-1]:
    print "yes, %s is a palindrome word" % (seq)
else:
    print "sorry, '%s' 'isn\'t a palindrome word" % (seq)

plaindrome_long.py

array = list(raw_input("\nPlease input string to test for plaindrome condition.\n").lower())

while len(array) > 1:
    if array[0] == array[-1]:
        array.pop(
        array.pop()
    else:
        break

if len(array) == 0 or len(array) == 1:
    print "Your string IS a palindrom"
else:
    print "Your string IS NOT a plaindrom"


{{CODECanaryJavaScript}}

Leave a Reply

Your email address will not be published.