From: NilsForssen Date: Wed, 2 Mar 2022 15:51:08 +0000 (+0100) Subject: update X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=6fde018a1f9ca7f2488e3a0e9e54618edab0d684;p=TDDE44.git update --- diff --git a/laboration4/__pycache__/adventure.cpython-38.pyc b/laboration4/__pycache__/adventure.cpython-38.pyc new file mode 100644 index 0000000..cbf667e Binary files /dev/null and b/laboration4/__pycache__/adventure.cpython-38.pyc differ diff --git a/laboration4/__pycache__/common.cpython-38.pyc b/laboration4/__pycache__/common.cpython-38.pyc index e6a5c61..3d2861e 100644 Binary files a/laboration4/__pycache__/common.cpython-38.pyc and b/laboration4/__pycache__/common.cpython-38.pyc differ diff --git a/laboration4/__pycache__/gamedata.cpython-38.pyc b/laboration4/__pycache__/gamedata.cpython-38.pyc new file mode 100644 index 0000000..af7b35d Binary files /dev/null and b/laboration4/__pycache__/gamedata.cpython-38.pyc differ diff --git a/laboration4/__pycache__/pictures.cpython-38.pyc b/laboration4/__pycache__/pictures.cpython-38.pyc new file mode 100644 index 0000000..6e7f3a9 Binary files /dev/null and b/laboration4/__pycache__/pictures.cpython-38.pyc differ diff --git a/laboration4/__pycache__/uppgift_1.cpython-38.pyc b/laboration4/__pycache__/uppgift_1.cpython-38.pyc new file mode 100644 index 0000000..328b4d1 Binary files /dev/null and b/laboration4/__pycache__/uppgift_1.cpython-38.pyc differ diff --git a/laboration4/__pycache__/uppgift_2.cpython-38.pyc b/laboration4/__pycache__/uppgift_2.cpython-38.pyc new file mode 100644 index 0000000..e0c5b38 Binary files /dev/null and b/laboration4/__pycache__/uppgift_2.cpython-38.pyc differ diff --git a/laboration4/adventure.py b/laboration4/adventure.py new file mode 100644 index 0000000..21c5932 --- /dev/null +++ b/laboration4/adventure.py @@ -0,0 +1,248 @@ +#!/usr/bin/env python +# +# A text-based adventure game, based on +# https://github.com/codinggrace/text_based_adventure_game +# +# MIT License +# Copyright (c) 2020 Coding Grace +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from pictures import * +from gamedata import * + + +def get_next_state(curr_state): + succ = ADVENTURE_TREE.get(curr_state) + if len(succ) > 1: + for i, s in enumerate(succ): + print(f"{i + 1} {OPTIONS.get(s)}") + inp = input() + return succ[int(inp) - 1] + return succ[0] + +def print_desc(curr_state): + print(DESCRIPTIONS.get(curr_state)) + +def print_pic(curr_state): + pic = PICTURES.get(curr_state) + if pic is not None: + pic() + +def main(): + name = input("What's your name?\n>> ") + print("Welcome {} to the adventure of your life. Try to survive and find \ + the treasure!".format(name.upper())) + current_state = "Start" + while current_state != "End": + print_pic(current_state) + print_desc(current_state) + current_state = get_next_state(current_state) + + +def main_old(): + current_state = "Start" + succ_states = ADVENTURE_TREE[current_state] + + name = input("What's your name?\n>> ") + print("Welcome {} to the adventure of your life. Try to survive and find the \ + treasure!".format(name.upper())) + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + print_doors() + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Blue" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS["Blue"], + "1", OPTIONS["Chest"], + "2", OPTIONS["Guard"]) + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Chest" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + print_treasure() + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Take" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Sneak" + succ_states = ADVENTURE_TREE[current_state] + + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + + print_guard() + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Sneak_right" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print(text_box) + + elif inp == "2": + current_state = "Sneak_left" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print_ded() + print(text_box) + print_game_over() + + + elif inp == "2": + current_state = "Talk" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print_guard() + print(text_box) + print_game_over() + + elif inp == "2": + current_state = "Leave" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Sneak" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + + print_guard() + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Sneak_right" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print(text_box) + + elif inp == "2": + current_state = "Sneak_left" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print_ded() + print(text_box) + print_game_over() + + + elif inp == "2": + current_state = "Talk" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print_guard() + print(text_box) + print_game_over() + + elif inp == "2": + current_state = "Guard" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Sneak" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + + print_guard() + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Sneak_right" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print(text_box) + + elif inp == "2": + current_state = "Sneak_left" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print_ded() + print(text_box) + print_game_over() + + + elif inp == "2": + current_state = "Talk" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print_guard() + print(text_box) + print_game_over() + + elif inp == "2": + current_state = "Red" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}\n{} {}\n{} {}".format(DESCRIPTIONS[current_state], + "1", OPTIONS[succ_states[0]], + "2", OPTIONS[succ_states[1]]) + print_dragon() + print(text_box) + inp = input(">> ") + + if inp == "1": + current_state = "Flee" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print(text_box) + + elif inp == "2": + current_state = "Attack" + succ_states = ADVENTURE_TREE[current_state] + text_box = "{}".format(DESCRIPTIONS[current_state]) + print(text_box) + print_game_over() + + +if __name__ == "__main__": + main() + diff --git a/laboration4/common.py b/laboration4/common.py index 008b743..91ab5bf 100644 --- a/laboration4/common.py +++ b/laboration4/common.py @@ -1,7 +1,6 @@ def load_csv(filename): - our_file = open(filename,"r") + our_file = open(filename, "r") file_read = our_file.readlines() our_file.close() return file_read - diff --git a/laboration4/figur.png b/laboration4/figur.png deleted file mode 100644 index d6e1d1d..0000000 Binary files a/laboration4/figur.png and /dev/null differ diff --git a/laboration4/figur1.png b/laboration4/figur1.png deleted file mode 100644 index 60ad725..0000000 Binary files a/laboration4/figur1.png and /dev/null differ diff --git a/laboration4/figur2.png b/laboration4/figur2.png deleted file mode 100644 index b36c7d5..0000000 Binary files a/laboration4/figur2.png and /dev/null differ diff --git a/laboration4/gamedata.py b/laboration4/gamedata.py new file mode 100644 index 0000000..6e632ef --- /dev/null +++ b/laboration4/gamedata.py @@ -0,0 +1,91 @@ +from pictures import * + +ADVENTURE_TREE = { + "Start": ["Blue", "Red"], + "Blue": ["Chest", "Guard"], + "Chest": ["Take", "Leave"], + "Take": ["Sneak", "Talk"], + "Sneak": ["Sneak_right", "Sneak_left"], + "Sneak_right": ["Win"], + "Sneak_left": ["Die"], + "Talk": ["Talk_good", "Talk_not_noice"], + "Leave": ["Sneak", "Take"], + "Guard": ["Sneak", "Talk", "Steal_axe"], + "Red": ["Flee", "Attack"], + "Flee": ["End"], + "Attack": ["Die"], + "Die": ["End"], + "Talk_good": ["Win"], + "Talk_not_noice": ["Die"], + "Win": ["End"], + "Steal_axe": ["Die"] +} + +PICTURES = { + "Start": print_doors, + "Sneak": print_guard, + "Sneak_left": print_ded, + "Die": print_game_over, + "Talk": print_guard, + "Red": print_dragon, + "Chest": print_treasure, + "Talk_not_noice": print_ded, + "Win": print_win +} + +DESCRIPTIONS = { + "Start": """You enter a room, and you see a blue door to your left and a \ +red door to your right. +Which door do you pick?""", + "Blue": """You see a room with a wooden treasure chest on the left, and a \ +sleeping guard on the right in front of the door. +What do you do?""", + "Chest": """Let's see what's in here... /grins +The chest creaks open, and the guard is still sleeping. That's one heavy \ +sleeper! You find some diamonds, a shiny sword, and lots of gold coins. +Do you take the treasure or leave it?""", + "Take": """Woohoo! Bounty and a shiney new sword. /drops your crappy \ +sword in the empty treasure chest. +Ooops! The noise has woken up the guard. +What do you do now?""", + "Leave": + """Leaving all the shinies behind hurts, but it feels safer for now. +Hopefully, it will still be here, right after you gets past this guard. +What do you do next?""", + "Guard": """The guard seems to be deep in sleep, but he has a mean \ +looking axe right beside him. +What do you do?""", + "Sneak": "The guard is sleepy, what you do? right or left", + "Talk": "The guard aproches you!", + "Red": """There you see the great evil Slathborg. +He, it, whatever stares at you and you go insane. +Do you flee for your life or attack it with your bare hands?""", + "Flee": """You made it out alive, alas empty handed.""", + "Attack": """You died. Well, at least the dragon thought you were tasty""", + "Sneak_left": "The guard sees you, U ded!", + "Sneak_right": "The guard is missing his left eye and you snek past, hooray!", + "End": "", + "Die": "", + "Talk_not_noice": "The guard tells you fuck off and die!", + "Talk_good": "You can leave peasant.", + "Win": "", + "Steal_axe": "The axe is glued to the guard and he wakes up, u ded!" +} + +OPTIONS = { + "Blue": "Blue", + "Red": "Red", + "Chest": "Explore the chest", + "Guard": "Advance toward the guard", + "Take": "Grab all of the treasures", + "Leave": "Leave them for another day", + "Sneak": "Try to sneak past the guard", + "Talk": "Talk to the guard", + "Flee": "Flee", + "Attack": "Attack", + "Sneak_left": "Sneak to the left of the guard", + "Sneak_right": "Sneak to the right of the guard", + "Talk_good": "Offer the guard some flowers and cake.", + "Talk_not_noice": "Talk with a danish accent.", + "Steal_axe": "Steal his axe and murder him in his sleep" +} diff --git a/laboration4/laboration4.py b/laboration4/laboration4.py deleted file mode 100644 index 9ddbb85..0000000 --- a/laboration4/laboration4.py +++ /dev/null @@ -1,50 +0,0 @@ -import matplotlib -import numpy as np -import matplotlib.pyplot as plt -import sys -import common - -matplotlib.use('AGG') -plt.figure() - -def main(): - try: - csv_file_path = sys.argv[1] - except IndexError: - raise ValueError("Need csv file argument") - d1 = common.load_csv(csv_file_path) - x,y = prepare_data(d1) - draw_diagram1(x,y) - plt.savefig("figur2.png") - return - -def prepare_data(data): - new_list = [] - x_values = [] - y_values = [] - for rader in data: - new_list.append(rader.split(";")) - - new_list = new_list[1:] - - for i, rad in enumerate(new_list): - for j, s in enumerate(rad): - new_list[i][j] = int(s.strip()) - - #[int(s.strip()) for rad in new_list[1:] for s in rad] - for rad in new_list: - x_values.append(rad[0]) - y_values.append(sum(rad[1:])) - return (x_values, y_values) - -def draw_diagram1(x_values, y_values): - plt.plot(x_values, y_values) - plt.ylabel("Cups of coffee") - plt.xlabel("Time of day") - - - -if __name__ == "__main__": - main() - - \ No newline at end of file diff --git a/laboration4/pictures.py b/laboration4/pictures.py new file mode 100644 index 0000000..eb503d5 --- /dev/null +++ b/laboration4/pictures.py @@ -0,0 +1,139 @@ +def print_doors(): + print() + print(r" _________________________________________________________ ") + print(r" /| -_- _- |\ ") + print(r"/ |_-_- _ -_- _- -| \ ") + print(r" | _- _-- | ") + print(r" | , | ") + print(r" | .-'````````'. '(` .-'```````'-. | ") + print(r" | .` | `. `)' .` | `. | ") + print(r" | / | () \ U / | () \ | ") + print(r" | | | ; | o T o | | ; | | ") + print(r" | | | ; | . | . | | ; | | ") + print(r" | | | ; | . | . | | ; | | ") + print(r" | | | ; | .|. | | ; | | ") + print(r" | | |____;_________| | | |____;_________| | ") + print(r" | | / __ ; - | ! | / `'() _ - | | ") + print(r" | | / __ () -| - | / __-- - | | ") + print(r" | | / __-- _ | _- _ - | / __--_ | | ") + print(r" |__|/__________________|___________|/__________________|__| ") + print(r" / _ - lc \ ") + print(r"/ -_- _ - _- _--- -_- -_ \ ") + print() + +def print_dragon(): + print() + print(r" | | ") + print(r" \ / \ / ") + print(r" -= .'> =- -= <'. =- ") + print(r" '.'. .'.' ") + print(r" '.'. .'.' ") + print(r" '.'.----^----.'.' ") + print(r" /'==========='\ ") + print(r" . / .-. .-. \ . ") + print(r" :'.\ '.O.') ('.O.' /.': ") + print(r" '. | | .' ") + print(r" '| / \ |' ") + print(r" \ (o'o) / ") + print(r" |\ /| ") + print(r" \('._________.')/ ") + print(r" '. \/|_|_|\/ .' ") + print(r" /'._______.'\ lc ") + print() + + +def print_treasure(): + print() + print(" _.--. ") + print(" _.-'_:-'|| ") + print(" _.-'_.-::::'|| ") + print(" _.-:'_.-::::::' || ") + print(" .'`-.-:::::::' || ") + print(" /.'`;|:::::::' ||_ ") + print(" || ||::::::' _.;._'-._ ") + print(" || ||:::::' _.-!oo @.!-._'-. ") + print(" ('. ||:::::.-!()oo @!()@.-'_.| ") + print(" '.'-;|:.-'.&$@.& ()$%-'o.'-U|| ") + print(" `>'-.!@%()@'@_%-'_.-o _.|'|| ") + print(" ||-._'-.@.-'_.-' _.-o |'|| ") + print(" ||=[ '-._.-+U/.-' o |'|| ") + print(" || '-.]=|| |'| o |'|| ") + print(" || || |'| _| '; ") + print(" || || |'| _.-'_.-' ") + print(" |'-._ || |'|_.-'_.-' ") + print(" '-._'-.|| |' `_.-' ") + print(" '-.||_/.-' ") + print() + + +def print_guard(): + print() + print(r" ___I___ ") + print(r" /= | #\ ") + print(r" /.__-| __ \ ") + print(r" |/ _\_/_ \| ") + print(r" (( __ \__)) ") + print(r" __ ((()))))()) __ ") + print(r" ,' |()))))(((()|# `. ") + print(r" / |^))()))))(^| =\ ") + print(r" / /^v^(())()()v^;' .\ ") + print(r" |__.'^v^v^))))))^v^v`.__| ") + print(r" /_ ' \______(()_____( | ") + print(r" _..-' _//_____[xxx]_____\.-| ") + print(r" /,_#\.=-' /v^v^v^v^v^v^v^v^| _| ") + print(r" \)|) v^v^v^v^v^v^v^v^v| _| ") + print(r" || :v^v^v^v^v^v`.-' |# \, ") + print(r" || v^v^v^v`_/\__,--.|\_=_/ ") + print(r" >< :v^v____| \_____|_ ") + print(r" , || v^ / \ / ") + print(r" //\_||_)\ `/_..-._\ )_...__\ ") + print(r" || \/ #| |_='_( | =_(_ ") + print(r" || _/\_ | / =\ / ' =\ ") + print(r" \\\/ \/ )/ |=____#| '=....#| ") + print() + + +def print_game_over(): + print() + print(r" _____ __ __ ______ ______ ________ _____ ") + print(r" / ____| /\ | \/ | ____| / __ \ \ / / ____| __ \ ") + print(r" | | __ / \ | \ / | |__ | | | \ \ / /| |__ | |__) | ") + print(r" | | |_ | / /\ \ | |\/| | __| | | | |\ \/ / | __| | _ / ") + print(r" | |__| |/ ____ \| | | | |____ | |__| | \ / | |____| | \ \ ") + print(r" \_____/_/ \_\_| |_|______| \____/ \/ |______|_| \_\ ") + print() + + +def print_ded(): + print() + print(r" _ ___I___ ") + print(r" /\_/ \_/\ /= | #\ ") + print(r" / \ /.__-| __ \ ") + print(r" || || |/ _\_/_ \| ") + print(r" \ _ _ / (( __ \__)) ") + print(r" \/ || \/ __ ((()))))()) __ ") + print(r" || ,' |()))))(((()|# `. ") + print(r" || / |^))()))))(^| =\ ") + print(r" || / /^v^(())()()v^;' .\ ") + print(r" || |__.'^v^v^))))))^v^v`.__| ") + print(r" || /_ ' \______(()_____( | ") + print(r" ||.-' _//_____[xxx]_____\.-| ") + print(r" /,_#\.=-' /v^v^v^v^v^v^v^v^| _| ") + print(r" \)|) v^v^v^v^v^v^v^v^v| _| ") + print(r" :v^v^v^v^v^v`.-' |# \, ") + print(r" v^v^v^v`_/\__,--.|\_=_/ ") + print(r" :v^v____| \_____|_ ") + print(r" v^ / \ / ") + print(r" `/_..-._\ )_...__\ ") + print(r" |_='_( | =_(_ ") + print(r" / =\ / ' =\ ") + print(r" |=____#| '=....#| ") + print() + +def print_win(): + print(r" __ __ __ __ _ ") + print(r" \ \ / / \ \ / / | |") + print(r" \ \_/ /__ _ _ \ \ /\ / /__ _ __ | |") + print(r" \ / _ \| | | | \ \/ \/ / _ \| '_ \| |") + print(r" | | (_) | |_| | \ /\ / (_) | | | |_|") + print(r" |_|\___/ \__,_| \/ \/ \___/|_| |_(_)") \ No newline at end of file diff --git a/laboration4/uppgift_1.png b/laboration4/uppgift_1.png new file mode 100644 index 0000000..e7ab72d Binary files /dev/null and b/laboration4/uppgift_1.png differ diff --git a/laboration4/uppgift_1.py b/laboration4/uppgift_1.py new file mode 100644 index 0000000..6fe0806 --- /dev/null +++ b/laboration4/uppgift_1.py @@ -0,0 +1,50 @@ +import matplotlib +import numpy as np +import matplotlib.pyplot as plt +import sys +import common + + +def main(): + matplotlib.use('AGG') + plt.figure() + try: + csv_file_path = sys.argv[1] + except IndexError: + raise ValueError("Need csv file argument") + d1 = common.load_csv(csv_file_path) + x, y = prepare_data(d1) + draw_diagram1(x, y) + plt.ylabel("Cups of coffee") + plt.xlabel("Time of day") + plt.savefig("uppgift_1.png") + return + + +def prepare_data(data): + new_list = [] + x_values = [] + y_values = [] + for rader in data: + new_list.append(rader.split(";")) + + new_list = new_list[1:] + + for i, rad in enumerate(new_list): + for j, s in enumerate(rad): + new_list[i][j] = int(s.strip()) + + # [int(s.strip()) for rad in new_list[1:] for s in rad] + for rad in new_list: + x_values.append(rad[0]) + y_values.append(sum(rad[1:])) + return (x_values, y_values) + + +def draw_diagram1(x_values, y_values): + plt.plot(x_values, y_values) + + + +if __name__ == "__main__": + main() diff --git a/laboration4/uppgift_2.png b/laboration4/uppgift_2.png new file mode 100644 index 0000000..4e7de5b Binary files /dev/null and b/laboration4/uppgift_2.png differ diff --git a/laboration4/uppgift_2.py b/laboration4/uppgift_2.py new file mode 100644 index 0000000..025a68e --- /dev/null +++ b/laboration4/uppgift_2.py @@ -0,0 +1,40 @@ +import common +import sys +import matplotlib +import matplotlib.pyplot as plt + +def main(): + matplotlib.use('AGG') + plt.figure() + try: + csv_file_path = sys.argv[1] + except IndexError: + raise ValueError("Need csv file argument") + data = common.load_csv(csv_file_path) + x, y = prepare_data2(data) + draw_diagram2([x, y], csv_file_path[4:]) + plt.ylabel("Antal koppar") + plt.xlabel("Koppar kaffe per dag") + plt.legend(loc='upper left', bbox_to_anchor=(1, 0, 1, 1)) + plt.savefig("uppgift_2.png", bbox_inches="tight") + + + +def prepare_data2(d): + days = d[0].split(';') + cups_per_day = [0] * 7 + data = d[1:] + for line in data: + for day_i, cups in enumerate(line.split(';')[1:]): + cups_per_day[day_i] += int(cups) + days.pop(0) + days = [day.strip() for day in days] + return [days, cups_per_day] + + +def draw_diagram2(values, name): + plt.plot(values[0], values[1], label=name) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/laboration4/uppgift_3.png b/laboration4/uppgift_3.png new file mode 100644 index 0000000..1ab45e3 Binary files /dev/null and b/laboration4/uppgift_3.png differ diff --git a/laboration4/uppgift_3.py b/laboration4/uppgift_3.py new file mode 100644 index 0000000..d5dc260 --- /dev/null +++ b/laboration4/uppgift_3.py @@ -0,0 +1,29 @@ +import common +import uppgift_1 +import uppgift_2 +import matplotlib +import matplotlib.pyplot as plt +import sys + +matplotlib.use("AGG") +plt.figure() + +def main(): + arg_len = len(sys.argv) - 1 + csv_files = list() + for i in range(arg_len): + csv_files.append(sys.argv[i + 1]) + + for filer in csv_files: + data = common.load_csv(filer) + # uppgift_1.draw_diagram(uppgift_1.prepare_data(data)) + uppgift_2.draw_diagram2(uppgift_2.prepare_data2(data), filer[4:]) + + plt.xlabel("Dagar") + plt.ylabel("Antal koppar") + plt.legend(loc='upper left', bbox_to_anchor=(1, 0, 1, 1)) + plt.savefig(__file__[0:-3] + ".png", bbox_inches="tight") + + +if __name__ == "__main__": + main()