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 […]

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. […]

Lession3: Convert Hex-Dec-Oct-Bin

http://perltips.wikidot.com/convert-hex-dec-oct-bin use Perl to realize the convert among hex, dec, oct and bin. ############################## # convert hex to binary addr # ############################## # $short_ID[2] = 1fH chop($short_ID[2]); # $short_ID[2] = 1f my @bin_judg = split(/[\t]*/, $short_ID[2]); if(@bin_judg==2) { # it’s hex format: 1fH $short_ID[2] = sprintf( “%b”, hex( $short_ID[2])); @bin_judg = split(/[\t]*/, $short_ID[2]); } if(@bin_judg==5) […]

Lession2: Perl Arrays

Link: http://www.tutorialspoint.com/perl/perl_arrays.htm #!/usr/bin/perl (just used for note, actually we can ignore it with no side effects) @ages = (25,30,40); @names = (“John Paul”, “Lisa”, “Kumar”); print “\$ages[0] = $ages[0]\n”; print “\$ages[1] = $ages[1]\n”; print “\$ages[2] = $ages[2]\n”; print “\$names[0] = $names[0]\n”; print “\$names[1] = $names[1]\n”; print “\$names[2] = $names[2]\n”; Attention: use backslash(\) before $ sign to […]