# find unique number among array

Given an array, find the unique number which shows only once whereas other numbers show 3 times. Total Code: import java.util.*; class findUniq { public static int findUniq(int[] arr) { int[] tmp = new int[32]; for(int i = 0; i< arr.length; i++) { for(int j = 31; j >= 0 && arr[i] != 0; j–) […]

# TOP K Frequent elements in an unsorted array

Given an unsorted array, count its frequency, output its top k frequent elements Solution1: Use HashMap + TreeMap with customized comparator import java.util.*; class ValueComparator implements Comparator { Map base; public ValueComparator(Map base) { this.base = base; } public int compare(Integer a, Integer b) { if(base.get(a) >= base.get(b)) { return -1; } else { return […]

# LeetCode192: Word Frequency

Link: https://leetcode.com/problems/word-frequency/ assume that words.txt has the following content: the day is sunny the the the sunny is is Your script should output the following, sorted by descending frequency: the 4 is 3 sunny 2 day 1 # Read from the file words.txt and output the word frequency list to stdout. cat words.txt | tr […]

# LeetCode 202 Happy Hour

Link: https://leetcode.com/problems/happy-number/ A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those […]

# LeetCode 193: Valid Phone Numbers

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers. You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit) You may also assume […]

# Lesson6: Backend verification given 5-stage pipeline CPU

Given MIPS instructions file(cmd.txt), the output file of waveform(verify.csv), verify if it is matched between the computation by perl and the signals from waveforms. #!/usr/local/bin/perl use strict; use warnings; sub substore_data; sub subout_analysis; sub substorei; sub substore; sub subload; sub suband; sub subandi; sub subor; sub subori; sub subxor; sub subxori; sub subnop; sub subadd; […]

# Lesson5: Frontend given 5-stage pipeline CPU

1. Given MIPS instructions (cmd.txt), solve RAW hazards by adding NOPs among different instructions, then convert the updated instructions file with no RAW hazards to the vector file (0/1 signals). 2. MIPS instructions below contains: STOREI, STORE, LOAD, AND, ANDI, OR, ORI, XOR, XORI, NOP, ADD, ADDI, MUL, MULI. 3. No forwarding in the code. […]