cpsc 490 problem solving in computer science
play

CPSC 490 Problem Solving in Computer Science Introduction Lucca - PowerPoint PPT Presentation

CPSC 490 Problem Solving in Computer Science Introduction Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/07 University of British Columbia Coordinators: Lucca Siaudzionis ICPC World Finalist in 2018 and 2017 Interned twice at


  1. CPSC 490 Problem Solving in Computer Science Introduction Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/07 University of British Columbia

  2. Coordinators: Lucca Siaudzionis • ICPC World Finalist in 2018 and 2017 • Interned twice at Airbnb and once at Google • Two-time Silver medallist in NAIPC (2018 and 2017) • IOI Silver (2016) and Bronze (2015) Medallist • Wesbrook Scholar 1

  3. Coordinators: Jack Spalding-Jamieson • Published algorithms researcher • CRA Outstanding Undergraduate Researcher Honourable Mention • ICPC regional placements of 1st (Div. 2), 40th, 10th • Honourable mention in NAIPC (2018) • Interned at Jane Street 2

  4. Why You Should Take This Course • Learn useful algorithms and data structures not taught in any other course in UBC • Implement those algorithms and see them working • Practice problem solving, coding, and debugging • Rock at programming competitions and coding interviews 3

  5. Why You Should NOT Take This Course • Credits from CPSC 490 no longer satisfy the CPSC 4xx requirement for graduation (with exception of BA students) • Please check with the Computer Science department and/or your faculty to determine if this course can satisfy requirements in other programs. • If you think this is the only path to get a job 4

  6. Prerequisites • Knowledge of algorithms (especially from CPSC 320) will help you a lot, but it is not required • Being comfortable with Big-O analysis • Assignments may require writing code on C, C++, or Java. Python will be fine for most assignments, and extra languages may be provided on request 5

  7. Course Information Course website: students.cs.ubc.ca/~cs-490/2019W2/problem-solving • Proposal and syllabus • Lecture slides • Assignments • Presentation topics • Useful readings and links Piazza: https://piazza.com/ubc.ca/winterterm22019/cpsc490202204/home Access code: algorithms How to get help: after class or Piazza 6

  8. Grading Scheme • Assignment 1: 15% • Assignment 2: 15% • Assignment 3: 15% • Assignment 4: 15% • Assignment 5: 15% • Written Report: 15% • Presentation: 10% No exams! 7

  9. Assignments There will be 5 assignments, and they will have similar structure: • Each assignment will be a problem set • You have to write code to solve each problem • Our online judge will provide near-instant feedback on your solution • You can attempt each problem multiple times until the assignment deadline • There will be no partial marks for each problem 8

  10. Assignment Feedback Every time you attempt a solution, the judge will respond with one of the following: • Accepted: Congrats! You solved the problem • Wrong Answer: At least one of your outputs was incorrect • Time Limit Exceeded: Your solution is too slow • Memory Limit Exceeded: Your solution is consuming too much memory (recursing too much can lead to this) • Runtime Error: Can be a variety of errors, and most likely is a segmentation fault 9

  11. Written Report You will be required to write one detailed report this term: • You will pick one algorithm or data structure that really interests you • Each of you will have to pick a different topic • The report will be similar to a paper on it (but not as formal) • More details on this will be posted on the course website 10

  12. Presentation You will give one presentation to all of your classmates • The presentation will be on the same topic you chose for your written report • It will last around 12 minutes (plus questions) • The presentations will be after the due date of the report 11

  13. Academic Policy Using other people’s code • Try to code everything yourself • Make sure you understand everything your code does • Don’t blindly copy from the internet or other students. We will run plagiarism detectors Collaboration • Discuss ideas and approaches with your classmates! • Don’t, however, share code with anyone Citations • Cite all sources other than: lectures, slides, UBC/Stanford Code Archives • Cite everyone you collaborated with 12

  14. Input/Output (IO) Almost every problem in this course will use Standard Input/Output 13

  15. Standard IO C++ • cin / cout : easy to use but slow • De-sync with scanf/printf to get faster performance: ios base::sync with stdio(0); cin.tie(0); • Don’t flush buffer: cout << ’ \ n’ is faster than cout << endl • scanf / printf : faster than cin / cout Java • Scanner : versatile but slow • has nextBigInteger() , nextLine() , etc. • BufferedReader / Writer : fast, but have to parse manually Python • raw input / print : easy to use but slow • stdin / stdout : fast, slightly annoying to use 14

  16. Using cin/cout in C++ #include <iomanip> 1 #include <iostream> 2 using namespace std; 3 int main() { 4 // un-sync with scanf/printf for fast performance 5 ios_base::sync_with_stdio(0); cin.tie(0); 6 7 int a; long long b; double c; string s; 8 cin >> a >> b >> c >> s; 9 cout << setprecision(16) << fixed 10 << a << " " << b << " " << c << " " 11 << s << endl; 12 return 0; 13 } 14 15

  17. Using scanf/printf in C++ #include "stdio.h" 1 int main() { 2 int a; long long b; double c; char s[100]; 3 scanf("%d %lld %lf %s", &a, &b, &c, s); 4 printf("%d %lld %.16f %s\n", a, b, c, s); 5 return 0; 6 } 7 16

  18. Using Scanner in Java 1 import java.util.*; 2 public class A { public static void main(String[] args) { 3 Scanner sc = new Scanner(System.in); 4 5 int a = sc.nextInt(); 6 long b = sc.nextLong(); 7 double c = sc.nextDouble(); 8 String s = sc.next(); 9 10 System.out.println(a + " " + b + " " + 11 String.format("%.16f", c) + " " + s); 12 // OR 13 System.out.printf("%d %d %.16f %s\n", a, b, c, d); 14 15 sc.close(); 16 } 17 18 } 17

  19. Using BufferedReader in Java 1 import java.io.*; 2 import java.util.*; 3 public class A { public static void main(String[] args) { 4 BufferedReader reader = new BufferedReader( 5 new InputStreamReader(System.in)); 6 PrintWriter writer = new PrintWriter(System.out); 7 8 StringTokenizer toks = new StringTokenizer(reader.readLine()); 9 int a = Integer.parseInt(toks.nextToken()); 10 long b = Long.parseLong(toks.nextToken()); 11 double c = Double.parseDouble(toks.nextToken()); 12 String s = toks.nextToken(); 13 14 writer.println(a + " " + b + " " + 15 String.format("%.16f", c) + " " + s); 16 // OR 17 writer.printf("%d %d %.16f %s\n", a, b, c, d); 18 reader.close(); writer.close(); 19 } 20 21 } 18

  20. Using raw input() in Python def main(): 1 a, b, c, s = raw_input().split() 2 a = int(a) 3 b = int(b) 4 c = float("{0:.16f}".format(float(c))) 5 print("{} {} {} {}".format(a, b, c, s)) 6 # OR 7 print(a,b,c,s) 8 9 if __name__ == "__main__": 10 main() 11 19

  21. Using stdin/stdout in Python from sys import stdin, stdout 1 2 def main(): 3 a, b, c, s = stdin.readline().split() 4 a = int(a) 5 b = int(b) 6 c = float("{0:.16f}".format(float(c))) 7 stdout.write("{} {} {} {}\n".format(a, b, c, s)) 8 9 if __name__ == "__main__": 10 main() 11 20

  22. Sample Problems Let’s solve some problems! 21

  23. Problem 1 - Simple Multiplication Input: A single integer, n (1 ≤ n ≤ 10), followed by n lines containing each two integers a i and b i (1 ≤ a i , b i ≤ 10 3 ). Output: n lines, where the i -th line contains the value of a i × b i . Time Limit: 1 Second. 22

  24. Problem 2 - [Not So] Simple Multiplication Input: A single integer, n (1 ≤ n ≤ 1 , 000 , 000), followed by n lines containing each two integers a i and b i (1 ≤ a i , b i ≤ 10 7 ). Output: n lines, where the i -th line contains the value of a i × b i . Time Limit: 1 Second. 23

  25. Problems 1 & 2 - Sample Solution[?] #include <iostream> 1 using namespace std; 2 3 int main() { 4 int n; 5 cin >> n; 6 7 for (int i = 0; i < n; i++) { 8 int a, b; 9 cin >> a >> b; 10 cout << a * b << endl; 11 } 12 13 return 0; 14 } 15 24

  26. Sample Solution - What Could Go Wrong? • This will correctly solve Problem 1 • However, it will fail Problem 2 in two different ways • Overflows integers • Exceeds time limit 25

  27. Common Pitfall 1: Overflow • In, C++, an int stores values from − 2 31 to 2 31 − 1 ( ≈ 2 × 10 9 ) • Multiplying two int s of up to 10 7 can result in 10 14 • This overflow will lead to undesired behaviour (thus the wrong answer!) • How to fix this (in C++): • Easiest way would be to use long long s 26

  28. Common Pitfall 2: Time Limit Exceeded • Recall that each problem had a limit limit of 1 second • This requires reading input + calculating output + printing output to be in 1 second • Using cin / cout and endl can lead to a lot of unwanted time • How to fix this (in C++): • De-sync with scanf/printf to get faster performance: ios base::sync with stdio(0); cin.tie(0); • Don’t flush buffer: cout << ’ \ n’ is faster than cout << endl 27

Recommend


More recommend