Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hygull/0ea7d6fcbe315bcef3655810ae2f2f4e to your computer and use it in GitHub Desktop.
Save hygull/0ea7d6fcbe315bcef3655810ae2f2f4e to your computer and use it in GitHub Desktop.
A simple hashing problem(from hackerearh practice section) to choose a name from a hash table created by hygull - https://repl.it/EyA0/2
/*
#Created on : 29/12/2016.
#Aim of program : A simple hashing problem to choose a name from a hash table.
#Coded by : Rishikesh Agrawani.
#Problem's link : https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/
*/
package main
import "fmt"
func main() {
/*
Visit to see the problem =>
https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/
*/
var n int //Number of colleagues
var rollNumber int //roll number of colleague
var queries int//Number of queries that Monk can ask to helper
var name string //Name of colleague
fmt.Scanf("%d",&n)
hash :=make(map[int]string) //A hash table to map names from rollnumbers
for i:=0;i<n;i++{
fmt.Scanf("%d%s",&rollNumber,&name)
hash[rollNumber]=name //Storing key-value pairs
}
fmt.Scanf("%d",&queries)
var requestedRollNumbers []int //A list of roll numbers that Monk wants to know
for i:=0;i<queries;i++{
fmt.Scanf("%d",&rollNumber)
requestedRollNumbers=append(requestedRollNumbers,rollNumber)
}
for _,rollNumber:=range requestedRollNumbers{
fmt.Println(hash[rollNumber]) //Displaying the names for all queries
}
}
/*OUTPUT(As hackerearth wants...Visit the above mentioned link in comment)
5
1 bhagya
2 hemkesh
3 malinikesh
4 chotti
5 glue
3
3
4
2
malinikesh
chotti
hemkesh
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment