Skip to content

Instantly share code, notes, and snippets.

@Hamleyburger
Last active January 4, 2021 17:44
Show Gist options
  • Save Hamleyburger/8491dd8f8b3e826491310f64f422f10b to your computer and use it in GitHub Desktop.
Save Hamleyburger/8491dd8f8b3e826491310f64f422f10b to your computer and use it in GitHub Desktop.
Solution for pset7 houses, CS50 2020
import sqlite3
import csv
import sys
def main():
# checking argv (that there's one and it's a csv)
if (len(sys.argv) != 2):
sys.exit("Usage: import.py file.csv")
filename = sys.argv[1]
if not (filename.endswith(".csv")):
sys.exit("You must provide a *.csv")
# Connect with the .db file and make a cursor
sqlite_file = "students.db"
con = sqlite3.connect(sqlite_file)
cur = con.cursor()
# Open the csv file to import from
with open(filename, "r") as characters:
# Make a dictionary reader that iterates through rows
reader = csv.DictReader(characters)
for row in reader:
names = []
for part in row["name"].split(" "):
names.append(part)
names.append(row["house"])
names.append(row["birth"])
if (len(names) == 5):
cur.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)", names[:5])
if (len(names) == 4):
cur.execute("INSERT INTO students (first, last, house, birth) VALUES(?, ?, ?, ?)", names[:4])
con.commit()
con.close()
if __name__ == "__main__":
main()
import sqlite3
import csv
import sys
def main():
# checking argv (needs to be one, house name)
if (len(sys.argv) != 2):
sys.exit("Usage: roster.py house_name")
# Make house name lower to avoid cap sensitivity
housename = sys.argv[1].lower()
# Check if argument is indeed a house at Hogwarts
houses = ["slytherin", "gryffindor", "ravenclaw", "hufflepuff"]
if housename.lower() not in houses:
sys.exit("provide house name: Gryffindor, Hufflepuff, Slytherin or Ravenclaw.")
# Connect with the .db file and make a cursor
sqlite_file = "students.db"
con = sqlite3.connect(sqlite_file)
cur = con.cursor()
cur.execute('SELECT first, middle, last, birth FROM students WHERE lower(house) = "{}" ORDER BY last, first;'.format(housename))
# Fetchall gives us all the rows of the table as a list of tuples with strings.
houseroster = cur.fetchall()
# Do stuff with each row in table
for row in houseroster:
if not row[1]:
print("{} {}, born {}".format(row[0], row[2], row[3]))
else:
print("{} {} {}, born {}".format(row[0], row[1], row[2], row[3]))
con.close()
if __name__ == "__main__":
main()
@sundus123850
Copy link

sundus123850 commented Apr 27, 2020 via email

@shobhit5923
Copy link

Here it is #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <ctype.h> #include "dictionary.h" // Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node *next; } node; // Number of buckets in hash table const unsigned int N = 26; // Hash table node *table[N]; char word_char[LENGTH + 1]; int counter = 0; // Returns true if word is in dictionary else false bool check(const char *word) { node *wordcheck; // find out which bucket int bucket = hash(word); wordcheck = table[bucket]; while (wordcheck != NULL) { if (strcasecmp(wordcheck->word, word) == 0) { return true; } wordcheck = wordcheck->next; } return false; } // Hashes word to a number unsigned int hash(const char word) { int ind = 0; for (int i = 0; word[i] != '\0'; i++) { ind += tolower(word[i]); } return ind % N; } bool load(const char dictionary) { FILE dic; dic = fopen(dictionary, "r"); if (dic == NULL) { return false; } while (fscanf(dic, "%s\n", word_char) != EOF) { node add = malloc(sizeof(node)); if (add == NULL) { return 1; } strcpy(add->word, word_char); int hashindex = hash(word_char); if (table[hashindex] == NULL) { table[hashindex] = add; add->next = NULL; } else { add->next = table[hashindex]; table[hashindex] = add; } counter++; } fclose(dic); return true; } // Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { return counter; } // Unloads dictionary from memory, returning true if successful else false bool unload(void) { for (int i = 0; i < N; i++) { node unLoaded; unLoaded = table[i]; while (unLoaded) { node tmp = unLoaded; unLoaded = unLoaded->next; free(tmp); } table[i] = NULL; } return true; } On Tue, 28 Apr 2020 at 12:30 AM shobhit5923 notifications@github.com wrote:

@.
commented on this gist. ------------------------------ Which codes do you want? … <#m_3774285395403146782_> On Mon, 27 Apr 2020 at 11:42 AM Alma @.
> wrote: @Hamleyburger https://github.com/Hamleyburger commented on this gist. ------------------------------ @Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger , I haven't completed it. Actually m not able to understand that properly. If u r not having any issues can u share ur code with me? It would be of great help. If u could mail me the code at @.
If you can explain to me in a little more detail what you're having trouble with I'll be happy to help you understand better :) My code is the same as these files here. Other than that there's a given .csv file with charactersm houses and birth years, and a .db file which is to have one table filled with data taken from the .csv file. Okay, actually there was one tiny difference, which I've changed: in line 24, the file name was hard coded to be "characters.csv" - now it's just the file name supplied as an argument when running. That will have caused a problem for anyone whose file wasn't named "characters.csv" — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3271300, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR3CUR4776JSHGKGIBLROVAOBANCNFSM4LI3TJHQ . The pset5 has a speller code. That one i want — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272341, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR5WVTDLWGH3QV5FKDTROX2PHANCNFSM4LI3TJHQ .

Thanks a lot!

@sundus123850
Copy link

sundus123850 commented Apr 27, 2020 via email

@shobhit5923
Copy link

If you want any help-just let me know. On Tue, 28 Apr 2020 at 12:46 AM shobhit5923 notifications@github.com wrote:

@.**** commented on this gist. ------------------------------ Here it is #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <ctype.h> #include "dictionary.h" // Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node next; } node; // Number of buckets in hash table const unsigned int N = 26; // Hash table node table[N]; char word_char[LENGTH + 1]; int counter = 0; // Returns true if word is in dictionary else false bool check(const char word) { node wordcheck; // find out which bucket int bucket = hash(word); wordcheck = table[bucket]; while (wordcheck != NULL) { if (strcasecmp(wordcheck->word, word) == 0) { return true; } wordcheck = wordcheck->next; } return false; } // Hashes word to a number unsigned int hash(const char word) { int ind = 0; for (int i = 0; word[i] != '\0'; i++) { ind += tolower(word[i]); } return ind % N; } bool load(const char dictionary) { FILE dic; dic = fopen(dictionary, "r"); if (dic == NULL) { return false; } while (fscanf(dic, "%s\n", word_char) != EOF) { node add = malloc(sizeof(node)); if (add == NULL) { return 1; } strcpy(add->word, word_char); int hashindex = hash(word_char); if (table[hashindex] == NULL) { table[hashindex] = add; add->next = NULL; } else { add->next = table[hashindex]; table[hashindex] = add; } counter++; } fclose(dic); return true; } // Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { return counter; } // Unloads dictionary from memory, returning true if successful else false bool unload(void) { for (int i = 0; i < N; i++) { node unLoaded; unLoaded = table[i]; while (unLoaded) { node tmp = unLoaded; unLoaded = unLoaded->next; free(tmp); } table[i] = NULL; } return true; } On Tue, 28 Apr 2020 at 12:30 AM shobhit5923 @. @.> wrote: … <#m_3284242280885762563_> @. commented on this gist. ------------------------------ Which codes do you want? … <#m_3774285395403146782_> On Mon, 27 Apr 2020 at 11:42 AM Alma @.> wrote: @Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger commented on this gist. ------------------------------ @Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger , I haven't completed it. Actually m not able to understand that properly. If u r not having any issues can u share ur code with me? It would be of great help. If u could mail me the code at @. If you can explain to me in a little more detail what you're having trouble with I'll be happy to help you understand better :) My code is the same as these files here. Other than that there's a given .csv file with charactersm houses and birth years, and a .db file which is to have one table filled with data taken from the .csv file. Okay, actually there was one tiny difference, which I've changed: in line 24, the file name was hard coded to be "characters.csv" - now it's just the file name supplied as an argument when running. That will have caused a problem for anyone whose file wasn't named "characters.csv" — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3271300, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR3CUR4776JSHGKGIBLROVAOBANCNFSM4LI3TJHQ . The pset5 has a speller code. That one i want — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272341, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR5WVTDLWGH3QV5FKDTROX2PHANCNFSM4LI3TJHQ . Thanks a lot! — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR6KFTZYKRGBVUO3AIDROX4MPANCNFSM4LI3TJHQ .

Where to contact u?

@sundus123850
Copy link

sundus123850 commented Apr 27, 2020 via email

@shobhit5923
Copy link

Do you have Snapchat or instagram? On Tue, 28 Apr 2020 at 12:49 AM shobhit5923 notifications@github.com wrote:

@.**** commented on this gist. ------------------------------ If you want any help-just let me know. On Tue, 28 Apr 2020 at 12:46 AM shobhit5923 @.*** wrote: … <#m_-5544005420921328406_> @.**** commented on this gist. ------------------------------ Here it is #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <ctype.h> #include "dictionary.h" // Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node next; } node; // Number of buckets in hash table const unsigned int N = 26; // Hash table node table[N]; char word_char[LENGTH + 1]; int counter = 0; // Returns true if word is in dictionary else false bool check(const char word) { node wordcheck; // find out which bucket int bucket = hash(word); wordcheck = table[bucket]; while (wordcheck != NULL) { if (strcasecmp(wordcheck->word, word) == 0) { return true; } wordcheck = wordcheck->next; } return false; } // Hashes word to a number unsigned int hash(const char word) { int ind = 0; for (int i = 0; word[i] != '\0'; i++) { ind += tolower(word[i]); } return ind % N; } bool load(const char dictionary) { FILE dic; dic = fopen(dictionary, "r"); if (dic == NULL) { return false; } while (fscanf(dic, "%s\n", word_char) != EOF) { node add = malloc(sizeof(node)); if (add == NULL) { return 1; } strcpy(add->word, word_char); int hashindex = hash(word_char); if (table[hashindex] == NULL) { table[hashindex] = add; add->next = NULL; } else { add->next = table[hashindex]; table[hashindex] = add; } counter++; } fclose(dic); return true; } // Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { return counter; } // Unloads dictionary from memory, returning true if successful else false bool unload(void) { for (int i = 0; i < N; i++) { node unLoaded; unLoaded = table[i]; while (unLoaded) { node tmp = unLoaded; unLoaded = unLoaded->next; free(tmp); } table[i] = NULL; } return true; } On Tue, 28 Apr 2020 at 12:30 AM shobhit5923 @. @.**> wrote: … <#m_3284242280885762563_> @. commented on this gist. ------------------------------ Which codes do you want? … <#m_3774285395403146782_> On Mon, 27 Apr 2020 at 11:42 AM Alma @.> wrote: @Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger commented on this gist. ------------------------------ @Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger , I haven't completed it. Actually m not able to understand that properly. If u r not having any issues can u share ur code with me? It would be of great help. If u could mail me the code at @. If you can explain to me in a little more detail what you're having trouble with I'll be happy to help you understand better :) My code is the same as these files here. Other than that there's a given .csv file with charactersm houses and birth years, and a .db file which is to have one table filled with data taken from the .csv file. Okay, actually there was one tiny difference, which I've changed: in line 24, the file name was hard coded to be "characters.csv" - now it's just the file name supplied as an argument when running. That will have caused a problem for anyone whose file wasn't named "characters.csv" — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3271300, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR3CUR4776JSHGKGIBLROVAOBANCNFSM4LI3TJHQ . The pset5 has a speller code. That one i want — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272341, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR5WVTDLWGH3QV5FKDTROX2PHANCNFSM4LI3TJHQ . Thanks a lot! — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR6KFTZYKRGBVUO3AIDROX4MPANCNFSM4LI3TJHQ . Where to contact u? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272359, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR4WQBOX5HN6HON36A3ROX4XVANCNFSM4LI3TJHQ .

Both

@shobhit5923
Copy link

Do you have Snapchat or instagram? On Tue, 28 Apr 2020 at 12:49 AM shobhit5923 notifications@github.com wrote:

@.**** commented on this gist. ------------------------------ If you want any help-just let me know. On Tue, 28 Apr 2020 at 12:46 AM shobhit5923 @.*** wrote: … <#m_-5544005420921328406_> @.**** commented on this gist. ------------------------------ Here it is #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <ctype.h> #include "dictionary.h" // Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node next; } node; // Number of buckets in hash table const unsigned int N = 26; // Hash table node table[N]; char word_char[LENGTH + 1]; int counter = 0; // Returns true if word is in dictionary else false bool check(const char word) { node wordcheck; // find out which bucket int bucket = hash(word); wordcheck = table[bucket]; while (wordcheck != NULL) { if (strcasecmp(wordcheck->word, word) == 0) { return true; } wordcheck = wordcheck->next; } return false; } // Hashes word to a number unsigned int hash(const char word) { int ind = 0; for (int i = 0; word[i] != '\0'; i++) { ind += tolower(word[i]); } return ind % N; } bool load(const char dictionary) { FILE dic; dic = fopen(dictionary, "r"); if (dic == NULL) { return false; } while (fscanf(dic, "%s\n", word_char) != EOF) { node add = malloc(sizeof(node)); if (add == NULL) { return 1; } strcpy(add->word, word_char); int hashindex = hash(word_char); if (table[hashindex] == NULL) { table[hashindex] = add; add->next = NULL; } else { add->next = table[hashindex]; table[hashindex] = add; } counter++; } fclose(dic); return true; } // Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { return counter; } // Unloads dictionary from memory, returning true if successful else false bool unload(void) { for (int i = 0; i < N; i++) { node unLoaded; unLoaded = table[i]; while (unLoaded) { node tmp = unLoaded; unLoaded = unLoaded->next; free(tmp); } table[i] = NULL; } return true; } On Tue, 28 Apr 2020 at 12:30 AM shobhit5923 @. @.**> wrote: … <#m_3284242280885762563_> @. commented on this gist. ------------------------------ Which codes do you want? … <#m_3774285395403146782_> On Mon, 27 Apr 2020 at 11:42 AM Alma @.> wrote: @Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger commented on this gist. ------------------------------ @Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger https://github.com/Hamleyburger , I haven't completed it. Actually m not able to understand that properly. If u r not having any issues can u share ur code with me? It would be of great help. If u could mail me the code at @. If you can explain to me in a little more detail what you're having trouble with I'll be happy to help you understand better :) My code is the same as these files here. Other than that there's a given .csv file with charactersm houses and birth years, and a .db file which is to have one table filled with data taken from the .csv file. Okay, actually there was one tiny difference, which I've changed: in line 24, the file name was hard coded to be "characters.csv" - now it's just the file name supplied as an argument when running. That will have caused a problem for anyone whose file wasn't named "characters.csv" — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3271300, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR3CUR4776JSHGKGIBLROVAOBANCNFSM4LI3TJHQ . The pset5 has a speller code. That one i want — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272341, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR5WVTDLWGH3QV5FKDTROX2PHANCNFSM4LI3TJHQ . Thanks a lot! — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR6KFTZYKRGBVUO3AIDROX4MPANCNFSM4LI3TJHQ . Where to contact u? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/8491dd8f8b3e826491310f64f422f10b#gistcomment-3272359, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO46TR4WQBOX5HN6HON36A3ROX4XVANCNFSM4LI3TJHQ .

Text me on shobhit_dev . It's my insta account. So that If i got any issues i would be able to contact u

@sundus123850
Copy link

sundus123850 commented Apr 27, 2020 via email

@imrancs50
Copy link

It's not here. I actually don'y know how to share this repo with you, I think they're private through CS50. I can send the file to you if you still want.

HI,
would you please sent the file at imcisco@live.com or share anywhere

Thank you

@KevinKhang243
Copy link

For the houses code, i don't think it is correct solution ...

Copy link

ghost commented May 26, 2020

This is my code written with CS50 library in CS50 IDE.
Import.py
Screenshot from 2020-05-21 09-15-24

Roster.py
Screenshot from 2020-05-21 09-15-40
With no errors

Copy link

ghost commented May 26, 2020

@Hamleyburger But I didn't understood your code, there are many new things in your code.
what does this "cur = con.cursor()" means and why is it used here??
Also what is '.append()' and its importance.

@Hamleyburger
Copy link
Author

@vikash10246

Okay. The "cur" has to do with the sqlite3 library that I'm importing. Since I wanted to run this independently of CS50 IDE I use that instead. Using sqlite3 you have to first connect to the database:

con = sqlite3.connect(sqlite_file)

and then define a cursor that you use for reading through the database:

cur = con.cursor()

I execute some SQL commands with cur.execute() and eventually I commit. That's it.

.append() is used for lists and appends something to the end of the list and thus gives it +1 length.

for this to work the libraries must be imported and installed (e.g: $ pip install sqlite3)

Copy link

ghost commented May 27, 2020

@Hamleyburger
Thankyou, doubt is cleared now!!

@elhamhuq1
Copy link

@vikash10246
Why do you need middle = row["middle"].strip()?
Also shouldn't it be == None

Copy link

ghost commented Jun 15, 2020

@elhamhuq1
The strip() method removes characters from both left and right of the name(like extra space, dots, comma). If don't want to use this function then also there will be no problem, its your choice.
Now, here I just didn't use middle == None, instead I used a space in the else statement.

@ferialahmed
Copy link

from cs50 import SQL
import csv
from sys import argv
#open database
#open("students.db", "w").close()
db = SQL("sqlite:///students.db")
#db.execute("CREATE TABLE students (first TEXT, middle TEXT, last TEXT, house TEXT, birth NUMERIC)")

#checking length of command line arguments
if len(argv)!=2:
print("enter name of csv file")

exit()

#read from csv file
with open(argv[1], "r") as file:
reader=csv.DictReader(file, delimiter="\t")

names=[]
for row in reader:
names=(row["name"].split())
if len(names)==2:
f_name=names[0]
l_name=names[1]
db.execute("INSERT INTO students (first,middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
f_name, l_name, None, row["house"], int(row["birth"]))
if len(names)==3:
f_name=names[0]
m_name=names[1]
l_names=names[2]
db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
f_name, m_name, l_name, row["house"], row["birth"])

this is the import.py file, but i don't know what's wrong with it

@ferialahmed
Copy link

import csv
from sys import argv
from cs50 import SQL

if len(argv)!=2:
print("there is an error")
db = SQL("sqlite:///students.db")
table=db.execute("SELECT first,middle,last,birth WHERE house=argv[1] ORDER BY last,first")
for row in table:
if row["middle"]!=None:
print(row["first"] +" " + row["middle"] + " " + row["last"] + "born, " + row["born"])
else:
print(row["first"] +" " + row["last"] + "born, " + row["born"])
and here's the roster.py file, idon't know what's wrong too

@jahin07-ai
Copy link

Thanks a lott 😇😇
Actually I have completed the course 😋

Can I get the link of your final project?

@Shwesinnyein
Copy link

submit50 cs50/problems/2020/x/houses
I submitted this pset7 houses but complete progress is 67% why? If u know pls let me know,thanks

@mrstoastedsnowman
Copy link

Hi. I don't know if this will be helpful, or too late, as this thread was started a while ago, but if you look at the output from the check50, it states that they're inputting "students.csv" as their CSV file, not "characters.csv", as is given to us in the pset.

I had hardcoded "characters.csv" in my import.py, which only gave me a score of 1/6 from check50, as the data wasn't imported from CSV to students.db due to having the wrong CSV filename. I then realised that harcoding it wasn't great coding practice anyway, so I changed it to take the command line input instead. I then found that when I ran submit50, check50 didn't run at all and I got 0%! I eventually got it to run successfully by hardcoding "students.csv" into my code. Not great coding practice, but maybe that's the only way they can run check50, with a hardcoded file name within the code?

Hope it's OK to post this, as it's not necessarily an issue of someone's ability to complete the pset, just a feature of check50?

@lamzyyabdulk
Copy link

Hi. I don't know if this will be helpful, or too late, as this thread was started a while ago, but if you look at the output from the check50, it states that they're inputting "students.csv" as their CSV file, not "characters.csv", as is given to us in the pset.

I had hardcoded "characters.csv" in my import.py, which only gave me a score of 1/6 from check50, as the data wasn't imported from CSV to students.db due to having the wrong CSV filename. I then realised that harcoding it wasn't great coding practice anyway, so I changed it to take the command line input instead. I then found that when I ran submit50, check50 didn't run at all and I got 0%! I eventually got it to run successfully by hardcoding "students.csv" into my code. Not great coding practice, but maybe that's the only way they can run check50, with a hardcoded file name within the code?

Hope it's OK to post this, as it's not necessarily an issue of someone's ability to complete the pset, just a feature of check50?

Hey idk if you're gonna see this but hopefully you do! Can you please tell me how you did that, I've been stuck on this for the past 3 days. I get 1 out of 6 on check50. Even tho roster works I think its the same issue as you're with the characters and students CSV files, pleased pleased please anyone. I'm so exhausted AND just want to be done

@mrstoastedsnowman
Copy link

Hi.

I initially used:

with open("characters.csv", "r") as data:

to open the file, but then came across the problem of the course team having used "students.csv" in check50. This reminded me that hardcoding the filename was not great, so changed it to take argv[1] instead. However, I couldn't get check50 to "like" it, so I eventually went with:

with open("students.csv", "r") as data:

As much as I disliked hardcoding it again, this did get me through check50.

Good luck!

@lamzyyabdulk
Copy link

lamzyyabdulk commented Dec 19, 2020 via email

@mrstoastedsnowman
Copy link

No problem - I spent hours agonising over this problem, so I feel your pain!

Yes, absolutely, the input file that you will have as part of the Pset is characters.csv, so when you test the code yourself, that's the filename you'll need to use in import.py.

Then, just before you submit it via check50, change the input filename in import.py to students.csv, and it should pass check50.

Messy, but in the absence of being able to use the second command line argument as the input file (which I just couldn't get to work), it works.

Does that help?

@lamzyyabdulk
Copy link

lamzyyabdulk commented Dec 19, 2020 via email

@mrstoastedsnowman
Copy link

Not sure if we're allowed to post code, but I can tell you what I did:

with open("students.csv", "r") as data:

Then used csv.DictReader to read the data into a dictionary

Then iterated over each row in the dictionary, assigning the values from the dictionary to variables for name, house and year of birth (I used a function called split to separate the first, last and middle names into 3 separate values in a list)

Lastly, I wrote an if/else conditional that checked whether the length of the the names list was 2 or 3, and used an appropriate INSERT statement in either case to insert a new row into the table, referencing the variables listed above, but inserting None if the student has no middle name.

@ShyaamCR
Copy link

Pretty neat coding btw 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment