Skip to content

Commit be852e8

Browse files
committed
informatics adding
1 parent 6d219e2 commit be852e8

37 files changed

Lines changed: 132428 additions & 0 deletions

.DS_Store

0 Bytes
Binary file not shown.

Python_Informatics/.DS_Store

8 KB
Binary file not shown.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Assignment 1 \"Mean\" \n",
8+
"# author: Michael Galarnyk\n"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 8,
14+
"metadata": {
15+
"collapsed": false
16+
},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"Enter 1st floating point numeric:3.0\n"
23+
]
24+
}
25+
],
26+
"source": [
27+
"num1 = float(raw_input('Enter 1st floating point numeric:'))"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 9,
33+
"metadata": {
34+
"collapsed": false
35+
},
36+
"outputs": [
37+
{
38+
"name": "stdout",
39+
"output_type": "stream",
40+
"text": [
41+
"Enter 2nd floating point numeric:3.4\n"
42+
]
43+
}
44+
],
45+
"source": [
46+
"num2 = float(raw_input('Enter 2nd floating point numeric:'))"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 10,
52+
"metadata": {
53+
"collapsed": false
54+
},
55+
"outputs": [
56+
{
57+
"name": "stdout",
58+
"output_type": "stream",
59+
"text": [
60+
"Enter 3rd floating point numeric:2.3\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"num3 = float(raw_input('Enter 3rd floating point numeric:'))"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 11,
71+
"metadata": {
72+
"collapsed": false
73+
},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"Enter 4th floating point numeric:4.5\n"
80+
]
81+
}
82+
],
83+
"source": [
84+
"num4 = float(raw_input('Enter 4th floating point numeric:'))"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 12,
90+
"metadata": {
91+
"collapsed": false
92+
},
93+
"outputs": [
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"Enter 5th floating point numeric:3.4\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"num5 = float(raw_input('Enter 5th floating point numeric:'))"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": 13,
109+
"metadata": {
110+
"collapsed": false
111+
},
112+
"outputs": [
113+
{
114+
"name": "stdout",
115+
"output_type": "stream",
116+
"text": [
117+
"3.32\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"print (num1 + num2 + num3 + num4 + num5)/5.0"
123+
]
124+
}
125+
],
126+
"metadata": {
127+
"kernelspec": {
128+
"display_name": "Python 2",
129+
"language": "python",
130+
"name": "python2"
131+
},
132+
"language_info": {
133+
"codemirror_mode": {
134+
"name": "ipython",
135+
"version": 2
136+
},
137+
"file_extension": ".py",
138+
"mimetype": "text/x-python",
139+
"name": "python",
140+
"nbconvert_exporter": "python",
141+
"pygments_lexer": "ipython2",
142+
"version": "2.7.11"
143+
}
144+
},
145+
"nbformat": 4,
146+
"nbformat_minor": 0
147+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
Michael Galarnyk
3+
4+
1. Define a function named to_number(str) that takes a string as a parameter, converts it to an int value, and returns that int value.
5+
2. Define a function named add_two(n1, n2) that takes two ints as parameters, sums them, and then returns that int sum value.
6+
3. Define a function named cube(n) that takes numeric value as a parameter, cubes that value, and then returns that resulting numeric value.
7+
4. Use the above functions in one statement to take two string literals, convert them to ints, add them together, cube the result, and print the cubed value.
8+
9+
'''
10+
11+
def to_number(string):
12+
new_int = int(string)
13+
return new_int
14+
15+
def add_two(n1,n2):
16+
summation = n1 + n2
17+
return summation
18+
19+
def cube(n):
20+
cubed = n**3
21+
return cubed
22+
23+
print cube(add_two(to_number('6'),to_number('5')))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Michael Galarnyk
4+
5+
Given the following python statement…
6+
avg_str = 'Average value read: 0.72903'
7+
Use the find() method and string slicing
8+
to extract the potion of the string after
9+
the colon character and then use the float()
10+
function to convert the extracted string
11+
into a floating point value. Save your code
12+
in a file named “parse_float.py”.
13+
"""
14+
15+
avg_str = 'Average value read: 0.72903'
16+
start_pos = avg_str.find(':');
17+
18+
# 1 more since start_po is after :
19+
start_pos = start_pos + 1;
20+
21+
# taking the number portion of the string
22+
number_portion = avg_str[start_pos:];
23+
24+
# typecasting number_portion to float
25+
number_portion = float(number_portion)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Michael Galarnyk
4+
5+
Write a program called “process_numbers.py”
6+
that repeatedly reads numbers input by
7+
the user until the user types “done”.
8+
After the user has entered “done”,
9+
print out the total, count, maximum,
10+
minimum, and average of the entered numbers.
11+
"""
12+
user_input = ''
13+
total = 0;
14+
count = 0;
15+
maximum = None;
16+
minimum = None;
17+
while (user_input != 'done'):
18+
user_input = raw_input('Enter a number or enter done\n');
19+
20+
if user_input != 'done':
21+
total += float(user_input);
22+
if (user_input == 'done'):
23+
average = total / float(count);
24+
break;
25+
count += 1
26+
if maximum is None or user_input > maximum:
27+
maximum = user_input
28+
if minimum is None or user_input < minimum:
29+
minimum = user_input
30+
31+
32+
print 'count: %d \n' %count
33+
print 'average%g \n' %average
34+
print 'maximum:%s \n' %maximum
35+
print 'minimum:%s \n' %minimum
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Michael Galarnyk
4+
Assignment: Files, Lists, and Split
5+
6+
1. Write a program that prompts the user for a filename. Open the file, and read it one line at a time. For each line split the line into a list of words called line_list. For each word in the current line_list, look to see if it is in a list called script_list. If the word is not in script_list, add it to the script_list. Sort the script_list alphabetically.
7+
2. Within the same program define a function called freq_count(). This function accepts a str and a list of words as arguments. It traverses the list of words and searches each word and counts the occurrences of the substring str within each word. Print each word along with the number of substring occurrences found with the associated word.
8+
3. Test your program with the romeo.txt file that comes as a text file resource with our textbook. Your program should accept the filename and the substring str as input from the user. After reading the file to build and sort script_list, pass script_list into the freq_count() function.
9+
"""
10+
11+
def freq_count(user_string, unique_words):
12+
for word in unique_words:
13+
print word, ":", user_string.count(word);
14+
15+
text_file_name = raw_input('Enter a text file please \n');
16+
user_string = raw_input('What substring do you want ? \n')
17+
with open('romeo.txt', 'r') as f:
18+
script_list = [];
19+
line_list = [];
20+
for line in f:
21+
line_list.extend(line.split());
22+
23+
for word in line_list:
24+
if word not in script_list:
25+
script_list.append(word);
26+
27+
script_list.sort();
28+
29+
freq_count(user_string, script_list);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Michael Galarnyk
4+
Assignment: Message Frequency Count
5+
6+
1. Write a program that reads through the mail box data and when you find a line that starts with “From”, extract the address information from the line. Count the number of messages from each person by using a dictionary. Note that you might need to look at more than “From” because of duplicate instances of the address (hint: “From “ vs. “From:”).
7+
2. After all of the data has been read, print the person with the highest number of messages. To do this, create a list of tuples (count, email) from the dictionary, sort the list in reverse order and print out the person who has the highest number of messages.
8+
"""
9+
10+
d = {};
11+
12+
with open('Data_Files/mbox.txt', 'r') as f:
13+
14+
for line in f:
15+
line = line.translate(None, '!\'#$%*:+') # removing punctuation
16+
line = line.lower().split()
17+
try:
18+
if line[0] == 'from':
19+
d[line[1]] = d.get(line[1],0) + 1
20+
except:
21+
pass
22+
23+
message_freq = [];
24+
25+
for key, val in d.items():
26+
message_freq.append((val,key));
27+
28+
message_freq.sort(reverse=True)
29+
30+
print 'The person with the highest number of messages is:', message_freq[0][1], ' with ', message_freq[0][0], 'messages'

Python_Informatics/6_URL_reader.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Michael Galarnyk
3+
4+
1. Rename the socket1.py program from our textbook to URL_reader.py.
5+
2. Modify the URL_reader.py program to use urllib instead of a socket.
6+
3. Add code that prompts the user for the URL so it can read any web page.
7+
4. Add error checking using try and except to handle the condition where the user enters an
8+
improperly formatted or non-existent URL.
9+
5. Count the number of characters received and stop displaying any text after it has shown 3000
10+
characters.
11+
6. Continue to retrieve the entire document, count the total number of characters, and display the
12+
total number of characters.
13+
'''
14+
# Wasnt sure what exactly the assignment wanted, but this seems to work.
15+
16+
import urllib
17+
import re
18+
19+
# https://www.gutenberg.org/files/1342/1342-h/1342-h.htm#link2HCH0034
20+
21+
try:
22+
url = raw_input('Enter - ');
23+
fhand = urllib.urlopen(url);
24+
count = 0; # only characters upper and lowercase a-z.
25+
adjusted_3000 = [];
26+
while count < 3000:
27+
new_char = fhand.read(1);
28+
adjusted_3000 = adjusted_3000 + re.findall('[A-Za-z]', new_char)
29+
count += 1;
30+
print 'first 3000 thousand characters are: ', adjusted_3000;
31+
32+
new_chars = fhand.read()
33+
entire_doc = adjusted_3000 + re.findall('[A-Za-z]', new_chars)
34+
print 'The length of the entire document is: ', len(entire_doc)
35+
except:
36+
print "Bad link"
37+
38+

Python_Informatics/7_geoxml.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# Michael Galarnyk
3+
# Assignment 7
4+
5+
# “Google Geocoding”
6+
# 1. Change either the geojson.py or geoxml.py program to print out the two-character country code from the retrieved data.
7+
# 2. Add error checking so your program does not traceback if the country code is not there.
8+
# 3. Use the program to search for “Pacific Ocean” and make sure that it can handle locations that
9+
# are not in any country.
10+
11+
import urllib
12+
import json
13+
14+
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
15+
16+
while True:
17+
address = raw_input('Enter location: ')
18+
if len(address) < 1 : break
19+
20+
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
21+
print 'Retrieving', url
22+
uh = urllib.urlopen(url)
23+
data = uh.read()
24+
print 'Retrieved',len(data),'characters'
25+
26+
try: js = json.loads(str(data))
27+
except: js = None
28+
if 'status' not in js or js['status'] != 'OK':
29+
print '==== Failure To Retrieve ===='
30+
print data
31+
continue
32+
33+
print json.dumps(js, indent=4)
34+
'''
35+
lat = js["results"][0]["geometry"]["location"]["lat"]
36+
lng = js["results"][0]["geometry"]["location"]["lng"]
37+
print 'lat',lat,'lng',lng
38+
''' #not necessary for this assignment
39+
location = js['results'][0]['formatted_address']
40+
print location
41+
42+
results = js['results'][0]
43+
address_components = results["address_components"]
44+
country = 0;
45+
for each_dict in address_components:
46+
types = each_dict["types"]
47+
if types == ["country", "political"]:
48+
country = 1;
49+
print "The two character country code is:", each_dict["short_name"]
50+
51+
if country == 0:
52+
print "Location isn't in any country"

0 commit comments

Comments
 (0)