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


# Cipher should eliminate capitalization, spaces & punctuation for maximum effectivness 
import string

def caesar():
    message = input("\nPlease input your message.\n").replace(" ","").lower().strip(string.punctuation)
    shift = int(input("What is the shift desired for the encoding?\n "))
    
    if (shift % 26 == 0):
        return("There can be no shift if divisible by 26")
    
    # decode option is simple math - inverse operations
    if "e" not in input("\nType 'e' or 'E' to encode, anything else will decode\n"):
        shift = len(string.ascii_letters) - shift
    
    alphabet = string.ascii_letters.lower()
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = str.maketrans(alphabet, shifted_alphabet)
    return message.translate(table)

if __name__ == '__main__':
	print(caesar())

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

palindrome_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)

palindrome_long.py

# Test for palindrome - Rats live on no evil star

def palindrome(message):
    while message:
        if message[0] == message[-1]:
            message.pop(0)
            message.pop()
        else:
            break

    if not message:
        print("Your string IS a palindrome")
    else:
        print("Your string IS NOT a palindrome")


if __name__ == '__main__':

    palindrome(list(input("\nInput string to test palindrome\n").replace(" ", "").lower()))