Lecture 3:
Bash Recap + Intro to Version Control
Fall 2024
Announcements
Outline
Outline
Bash Review
Exercise 1: Common Bash Commands
Let’s try doing these exercises together (Windows users: please use WSL + Ubuntu):�
Using the Command Line Cheat Sheet…
Answers Exercise 1: (Try it before you look)...
Bash + VIM Practice
Open a terminal | For Windows: Activate WSL using the command prompt PowerShell |
Navigate to csci338 | (or folder where you store csci338 files) |
From the command line create a file called readme.txt that has the text “Hello world!” | Use echo and either > (overwrite) or >> (append) operators. |
Verify that it worked using cat | |
Open a file called init-app.sh using vim | Command: vi init-app.sh |
Put vim into edit mode | Press the “i” key |
Create a script that:
| Use the course command line cheat sheet to help you. Hints:
|
Save your file and exit | First press escape. Then press: :wq |
Test the bash script you just made: | bash test.sh |
Answers Exercise 2: (Try it before you look)...
cd ~/Desktop/csci338 # should be a different path for you
echo "Hello world!" > readme.txt
cat readme.txt # should print "Hello world!" to the screen
vi init-app.sh
Within vim, go into edit mode by pressing the i key.
Then add the following lines to test.sh:
mkdir my-app
touch my-app/index.html
touch my-app/style.css
Save and quit vim by first pressing the Escape key and � then typing :wq to save and quit
bash init-app.sh # should have created a my-app directory + HTML/CSS file within your current directory
Editing your .zshrc file
Create a shortcut to your csci338 folder from the command line by editing your .zshrc file on your local computer. To do this:
Outline
Reminder of the Problem
Write a function – using any programming language you want – that takes in a list of integers. The function returns true if the list contains two or more of the same integer, false otherwise.
// Java
public static boolean containsPair(List<Integer> l) {
return false;
}
Suggested Approach
Also: You probably won’t be allowed to use a code editor when you’re doing coding interviews. Whiteboarding only!
containsPair: Approach #1
public static boolean containsPair(List<Integer> l) {
// first sort the list. Then do the thing below:
for (int i = 0; i < l.size() - 1; i++) {
if (l.get(i).equals(l.get(i + 1))) {
return true;
}
}
return false;
}
Q: Does this work? If not, what is the problem? Can you think of a scenario where this could work and a code enhancement you might make?
containsPair: Approach #2
public static boolean containsPair(List<Integer> l) {
for (int i = 0; i < (l.size() - 1); i++) {
for (int j = i + 1; j < l.size(); j++) {
if (l.get(i).equals(l.get(j))) {
return true;
}
}
}
return false;
}
Q: Does this work? Is it efficient? What’s the worst case scenario?
containsPair: Approach #3
public static boolean containsPair(List<Integer> l) {
List<Integer> unique = new ArrayList<Integer>();
for (int i = 0; i < l.size(); i++) {
if (unique.contains(l.get(i))) {
return true;
}
unique.add(l.get(i));
}
return false;
}
Q: Does this work? Is it efficient? What’s the worst case scenario?
containsPair: Approach #4
public static boolean containsPair(List<Integer> l) {
Map<Integer, Integer> s = new HashMap<Integer, Integer>();
for(int i = 0; i < l.size(); i++) {
if (s.containsKey(l.get(i))) {
return true;
}
s.put(l.get(i), 1);
}
return false;
}
Q: Does this work? Is it efficient? Do you think this is different than approach 3? Why or why not? What’s the worst case scenario?
containsPair: Approach #5
public static boolean containsPair(List<Integer> l) {
Set<Integer> s = new HashSet<Integer>(l);
return s.size() != l.size();
}
Q: Does this work? Is it efficient? What’s the worst case scenario?
Good Work!
Thinking like this takes lots of practice. Keep at it. We’ll keep doing these problems all semester.
URL Shortener
Your boss asks you to implement a URL shortener. Specifically:
What steps would you take to implement this system?
Let’s draw this out on the whiteboard…
Outline
Why is Code History important?
Why is Version Control Important?
What is the difference between centralized and distributed version control?
Centralized:
Distributed:
What does Google use and why?
What is the problem with having long-running dev branches? What is the solution?
What is the one version rule?
What are the tradeoffs of having a Monorepo versus multiple repos?
Outline
Intro to git
Git v. GitHub
30
How to Learn Git
The best way to learn git is to practice using git while working on a collaborative software project (like this class)!
There are also many great references you can draw from, including:
31
For Thursday