TutorMe homepage
Subjects
PRICING
COURSES
SIGN IN
Start Free Trial
Megan C.
Mechanical Engineering Masters Graduate
Tutor Satisfaction Guarantee
SQL Programming
TutorMe
Question:

Find most popular articles in a database.

Megan C.
Answer:

#!/usr/bin/env python3 import psycopg2 # Here we define the database we will be analyzing. NOTE: Even though the # SQL file is actually called newsdata.sql, we unpackaged it in the command # line using # psql -d news -f newsdata.sql # # This resulted in the renaming of our file which is now referred to as news DBNAME = "news" # Connect to the database and create cursor try: database = psycopg2.connect(database=DBNAME) c = database.cursor() except: print("""Cannot make connection. Database not found. Make sure you have downloaded it from the zip file provided in the README.md file, and have opened it using the command `psql -d news -f newsdata.sql` executed from the command line inside a virtual machine.""") # To write strings in multiple lines, we use the three quotations """x""" method # which also allows our editor to pick up on the PSQL command words # WHERE log.path LIKE '%' || articles.slug uses the general expression to match # patterns of strings to each other def popular_articles(cursor): """Returns top three most viewed articles.""" c.execute("""SELECT articles.title, count(*) AS views FROM articles, log WHERE log.path LIKE '%' || articles.slug GROUP BY articles.title ORDER BY views desc LIMIT 3;""") print("Most Popular Three Articles of All Time:") ans = c.fetchall() for a in ans: print (str(a[0].title()) + " - " + str(a[1]) + " views") print("") return ans def popular_authors(cursor): """Counts views each author received on sum of all articles.""" c.execute("""SELECT authors.name, count(*) AS views FROM articles, log, authors WHERE log.path LIKE '%' || articles.slug AND articles.author = authors.id GROUP BY authors.name ORDER BY views DESC;""") print("Most Popular Article Authors of All Time:") ans = c.fetchall() for a in ans: print (str(a[0].title()) + " - " + str(a[1]) + " views") print("") return ans def dooms_day(cursor): """Returns day of most errors.""" c.execute("""SELECT failed.date, failed.fail, (failed.fail + succeeded.success) AS total FROM (SELECT DATE(time), count(*) AS fail FROM log WHERE status != '200 OK' GROUP BY DATE(time)) AS failed, (SELECT DATE(time), count(*) AS success FROM log WHERE status = '200 OK' GROUP BY DATE(time)) AS succeeded WHERE failed.date = succeeded.date AND failed.fail*100 > (failed.fail + succeeded.success);""") print("Days That More Than 1 % of Requests Lead to Errors:") ans = c.fetchall() for a in ans: percentage = 100*a[1]/a[2] percentage = "{0:.3g}".format(percentage) print(str(a[0]) + ": " + str(percentage) + "% failure rate") print("") return ans popular_articles(c) popular_authors(c) dooms_day(c) database.close()

Python Programming
TutorMe
Question:

Play smoke or fire.

Megan C.
Answer:

import random import math name = input("Ahoy matie! What is your name? ") name = name.strip() name = name.lower() name = name.title() print("\nHello" + name + "!") gamer = input("Would you like to play a game? (yes/no) ") gamer = gamer.strip() gamer = gamer.lower() gamer = gamer[0] if gamer == 'n': print("\nListen bitch, you're going to play so don't be a dick. ") name = "Bitch" else: print("\nGreat sport!") wins = 0; #Game Number 1 first_answer = input("Smoke or Fire? ") first_answer = first_answer.lower() first_answer = first_answer.strip() card_num_1 = math.ceil(13*random.random()) suit_1 = math.ceil(4*random.random()) print() if suit_1 == 1: print(str(card_num_1) + " of Clubs") elif suit_1 == 2: print(str(card_num_1) + " of Diamonds") elif suit_1 ==3: print(str(card_num_1) + " of Spades") elif suit_1 == 4: print(str(card_num_1) + " of Hearts") if first_answer == "smoke": if suit_1 == 1|3: print("Nicely done mate!") wins = 1 else: print("Nope! Take a drink,",name,"!") else: if suit_1 == 2|4: print("Nicely done mate!") wins = 1 else: print("Nope! Take a drink",name,"!") #Game Number 2 card_num_2 = math.ceil(13*random.random()) suit_2 = math.ceil(4*random.random()) second_answer = input("Higher or Lower? ") second_answer = second_answer.strip() second_answer = second_answer[0] second_answer = second_answer.lower() if suit_2 == 1: print("\n" + str(card_num_2) + " of Clubs") elif suit_2 == 2: print("\n" + str(card_num_2) + " of Diamonds") elif suit_2 ==3: print("\n" + str(card_num_2) + " of Spades") elif suit_2 == 4: print("\n" + str(card_num_2) + " of Hearts") if second_answer == "h": if card_num_2 > card_num_1: print("Nicely Done!") wins = wins + 1 else: print("Yikes",name,"take a DRINK!") elif second_answer == "l": if card_num_2 < card_num_1: print("Nicely Done!") wins = wins + 1 else: print("Yikes",name,"take a DRINK!") else: print("Check your spelling, you drunkard, and take a drink.") #Game Number 3 card_num_3 = math.ceil(13*random.random()) suit_3 = math.ceil(4*random.random()) third_answer = input("Inside or Outside? ") third_answer = third_answer.strip() third_answer = third_answer.lower() third_answer = third_answer[0] if suit_3 == 1: print("\n" + str(card_num_3) + "of Clubs") elif suit_3 == 2: print("\n" + str(card_num_3) + "of Diamonds") elif suit_3 ==3: print("\n" + str(card_num_3) + "of Spades") elif suit_3 == 4: print("\n" + str(card_num_3) + "of Hearts") flag = 0 if third_answer == "i": if min(card_num_1,card_num_2) < card_num_3 < max(card_num_1,card_num_2): flag = 1 elif third_answer == "o": if not(min(card_num_1,card_num_2) < card_num_3 < max(card_num_1,card_num_2)): flag = 1 if flag == 1: print("You're killing it!") wins = wins + 1 else: print("Ooo, so close, but no. Take a drink!") #Game Number 4 #print() #print("Ok, last one. Here we go!") #suit = input("Choose a suit: ") #card_num_4 = math.ceil(13*random.random()) #suit_4 = math.ceil(4*random.random())

MATLAB
TutorMe
Question:

Simulate a bouncing ball.

Megan C.
Answer:

clear all; clc; clf; % Initialize Left_Wall = 0; Right_Wall = 1; % range in x Top_Wall = 1; Bottom_Wall = 0; % range in y t = 0; % initial time tfinal = 60; % final time in s dt_default = 0.03; % time step alpha = 0.8; % damping coefficient beta = 0.99; % friction coefficient g = 0.0981; % gravity constant Radius = 0.05; % radius of the ball in m x_old = 0.5; % x position of the ball in m y_old = 1-Radius; % y position of the ball in m u_old = 0.3; % initial velocity in the x-direction in m/s v_old = 0; % initial velocity in the y-direction in m/s a_x = 0; % acceleration in the x-direction in m/s^2 a_y = -g; % acceleration in the y-direction in m/s^2 dt = dt_default; hold on; while t < tfinal if t + dt > tfinal dt = tfinal-t; end u_new = u_old + dt*a_x; v_new = v_old + dt*a_y; x_new = x_old + dt*u_new; y_new = y_old + dt*v_new; % Treat the right wall: % if in the next time step, the ball will be going past the right wall, % we need to correct this by resetting x_new = right_wall - radius so % it remains within the bounds or our domain. However, in order to % maintain the laws of physics that are actually governing this system, % we must adjust all the other parameters. See what is really happening % here is not that we're resetting x_new, its that we are figuring out % what time step is needed to reach the wall and then using that time % step to calculate everything. To figure out the time step necessary % to reach the wall, you calculate the x_new at the wall and then use % it to calculate the new time step using the equation from above: % x_new = x_old + dt*u_new; and solving for dt. Next, we calculate the % remaining three variables using the appropriate dt. %RIGHT WALL if x_new + Radius >= Right_Wall x_new = Right_Wall - Radius; dt = abs(x_new-x_old)/abs(u_new); y_new = y_old + dt*v_new; u_new = u_old + dt*a_x; v_new = v_old + dt*a_y; u_new = -alpha*u_new; v_new = beta*v_new; end %LEFT WALL if x_new - Radius <= Left_Wall x_new = Left_Wall + Radius; dt = abs(x_new-x_old)/abs(u_new); y_new = y_old + dt*v_new; u_new = u_old + dt*a_x; v_new = v_old + dt*a_y; u_new = -alpha*u_new; v_new = beta*v_new; end %TOP WALL (CEILING) if y_new + Radius >= Top_Wall y_new = Top_Wall - Radius; dt = abs(y_new-y_old)/abs(v_new); x_new = x_old + dt*u_new; u_new = u_old + dt*a_x; v_new = v_old + dt*a_y; u_new = beta*u_new; v_new = -alpha*v_new; end %BOTTOM WALL (FLOOR) if y_new - Radius <= Bottom_Wall y_new = Bottom_Wall + Radius; dt = abs(y_new-y_old)/abs(v_new); x_new = x_old + dt*u_new; u_new = u_old + dt*a_x; v_new = v_old + dt*a_y; u_new = beta*u_new; v_new = -alpha*v_new; end Draw_Disk_lecture(x_new,y_new,Radius); axis([Left_Wall Right_Wall, Bottom_Wall Top_Wall]); axis square; pause(dt/10); t = t+dt; u_old = u_new; v_old = v_new; x_old = x_new; y_old = y_new; dt = dt_default; end

Send a message explaining your
needs and Megan will reply soon.
Contact Megan
Ready now? Request a lesson.
Start Session
FAQs
What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Session" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.