Saturday, January 23, 2016

CS 2420 - Assignment 1

So this semester I'm taking another programming class, CS 2420 called "Intro to Algorithms and Data Structures".

For the first assignment we had to write a few different functions using Python, which is great because its the language that I am the most familiar with it. Here is what I have:

# Christian Munoz
# Assignment 1
import sys
def swap_numbers(numbers, index1, index2):
"""This function takes in a list and two indexes to switch them in the list."""
temp = numbers[index1]
numbers[index1] = numbers[index2]
numbers[index2] = temp
def find_smallest(numbers):
"""This function finds the smallest number in a list."""
small = numbers[0]
for item in numbers:
if item < small:
small = item
return small
def sum_items(numbers):
"""This functions finds the sum of all numbers in a given list."""
total = 0
for item in numbers:
total += item
return total
def find_index(numbers, element):
"""This function will find the index for the element in the given list."""
index = 0
for item in numbers:
if element != item:
index += 1
elif element == item:
return index
def selection_sort(numbers):
"""This function will perform a selection sort on the given list."""
for index in range(0, len(numbers) - 1):
min_value = numbers[index]
for j in range(index + 1, len(numbers)):
if numbers[j] < min_value:
min_value = numbers[j]
if min_value != numbers[index]:
swap_numbers(numbers, find_index(numbers, min_value), index)
assert numbers == [1, 2, 5, 6, 7, 9], "SELECTION SORT: The list was not sorted correctly."
def insertion_sort(numbers):
"""This function will perform a insertion sort on the given list."""
for index in range(0, len(numbers) - 1):
current = numbers[index]
j = index
while j > 0 and numbers[j - 1] > current:
numbers[j] = numbers[j - 1]
j -= 1
numbers[j] = current
assert numbers == [1, 2, 5, 6, 7, 9], "INSERTION SORT: The list was not sorted correctly."
def main():
"""This is the function that will call all the other functions in the file."""
swap_list = [5, 2, 6, 7, 1, 9]
smallest_list = [5, 2, 6, 7, 1, 9]
sum_list = [5, 2, 6, 7, 1, 9]
index_list = [5, 2, 6, 7, 1, 9]
selection_list = [5, 2, 6, 7, 1, 9]
insertion_list = [5, 2, 6, 7, 1, 9]
swap_numbers(swap_list, 3, 5)
find_smallest(smallest_list)
sum_items(sum_list)
find_index(index_list, 5)
selection_sort(selection_list)
insertion_sort(insertion_list)
if __name__ == "__main__":
main()
sys.exit()
view raw Assignment1.py hosted with ❤ by GitHub

Friday, January 1, 2016

2048 Clone in Python

Let me start by saying Happy New Year to anyone that may be reading my blog!

So after I came back from my short vacation, I got sick and didn't have the energy to do anything. But I did have a chance to think for a while and I decided that I needed to get back into coding and brush up on my Python. The new semester starts in a bout a week or so and I need to get back into coding, even if I will be using Java in that class instead of Python. 

I decided that I should use Pygame (like I did a year ago in CS 1410 class) and create a simple game, and I decided to create a clone of the popular game 2048. I only started the other day and I have been working on getting a skeleton up and running and start planning on how I'm actually going to do this.

So far I have a simple screen with a "game board" displayed on the screen and I have started to work on the logic of the game before I do more "graphic" work. You can see my work on Github at the following link:


That's all for now, thanks for reading!