Skip to content

Commit dcf1cc5

Browse files
committed
add a few tc
1 parent 6b24478 commit dcf1cc5

File tree

13 files changed

+837
-0
lines changed

13 files changed

+837
-0
lines changed

hackerrank/story.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
6+
class Predictor():
7+
def __init__(self):
8+
self.text = raw_input().lower()
9+
self.sentences = self.text.split('.')
10+
11+
12+
def count_word_in_sent(self, word, sentence):
13+
words_list = ['his', 'her', 'he', 'she']
14+
cnt = 0
15+
for w in words_list:
16+
if sentence.find(w)!=-1:
17+
cnt += 1
18+
return cnt
19+
20+
def query(self, name):
21+
person = 0
22+
place = 0
23+
for n, sentence in enumerate(self.sentences):
24+
if sentence.find(name)!=-1:
25+
if n+1<len(self.sentences):
26+
next_sent_cnt = self.count_word_in_sent(name, self.sentences[n+1])
27+
if next_sent_cnt >=1:
28+
person += 1
29+
if sentence.find('in', 0, sentence.find(name)-1)!=-1:
30+
place += 2
31+
print place, person
32+
if person < place :
33+
print 'place'
34+
else:
35+
print 'person'
36+
37+
p = Predictor()
38+
N = int(raw_input())
39+
for i in range(N):
40+
p.query(raw_input().lower())
41+
42+

hackerrank/test

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Born and raised in a Hindu, merchant caste, family in coastal Gujarat, western India, and trained in law at the Inner Temple, London, Gandhi first employed non-violent civil disobedience as an expatriate lawyer in South Africa, in the resident Indian community's struggle for civil rights. After his return to India in 1915, he set about organising peasants, farmers, and urban labourers to protest against excessive land-tax and discrimination. Assuming leadership of the Indian National Congress in 1921, Gandhi led nationwide campaigns for easing poverty, expanding women's rights, building religious and ethnic amity, ending untouchability, but above all for achieving Swaraj or self-rule.Gandhi famously led Indians in challenging the British-imposed salt tax with the 400 km (250 mi) Dandi Salt March in 1930, and later in calling for the British to Quit India in 1942. He was imprisoned for many years, upon many occasions, in both South Africa and India. Gandhi attempted to practise non-violence and truth in all situations, and advocated that others do the same. He lived modestly in a self-sufficient residential community and wore the traditional Indian dhoti and shawl, woven with yarn hand spun on a charkha. He ate simple vegetarian food, and also undertook long fasts as means of both self-purification and social protest.In May 1883, the 13-year-old Mohandas was married to 14-year-old Kasturbai Makhanji (her first name was usually shortened to "Kasturba", and affectionately to "Ba") in an arranged child marriage, according to the custom of the region. In the process, he lost a year at school. Recalling the day of their marriage, he once said, "As we didn't know much about marriage, for us it meant only wearing new clothes, eating sweets and playing with relatives." However, as was prevailing tradition, the adolescent bride was to spend much time at her parents' house, and away from her husband. In 1885, when Gandhi was 15, the couple's first child was born, but survived only a few days. Gandhi's father, Karamchand Gandhi, had also died earlier that year. The religious background was eclectic. Gandhi's father was Hindu Modh Baniya and his mother was from Pranami Vaishnava family. Religious figures were frequent visitors to the home.
2+
7
3+
Gandhi
4+
Kasturba
5+
Gujarat
6+
London
7+
Karamchand
8+
South Africa
9+
Mohandas

interviews/a.out

24.6 KB
Binary file not shown.

interviews/answer.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
string number;
6+
7+
8+
9+
bool isValid(string num){
10+
int sz = num.size();
11+
12+
bool should_double = true;
13+
14+
int check_sum = 0;
15+
16+
for(int i=sz-2; i>=0; i--){
17+
int cur_digit = num[i]-'0'; //assume all are number digits
18+
19+
if(should_double==1){
20+
cur_digit *= 2;
21+
}
22+
should_double = !should_double;
23+
if(cur_digit>=10){
24+
int tmp = cur_digit%10;
25+
tmp += (cur_digit/10)%10;
26+
cur_digit = tmp;
27+
}
28+
check_sum += cur_digit;
29+
cout<<"adding digit "<<cur_digit<<endl;
30+
}
31+
check_sum += num[sz-1]-'0';
32+
33+
cout<<"debug: check sum = "<<check_sum<<endl;
34+
if(check_sum % 10 == 0){
35+
return true;
36+
}else{
37+
return false;
38+
}
39+
}
40+
41+
int main(){
42+
43+
if(isValid("")){
44+
cout<<"Number "<<number<<" is valid!"<<endl;
45+
}else{
46+
cout<<"not valid"<<endl;
47+
}
48+
return 0;
49+
}
50+

interviews/answer2.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
string number;
6+
7+
8+
int helper(string num){
9+
int sz = num.size();
10+
11+
bool should_double = true;
12+
13+
int check_sum = 0;
14+
15+
for(int i=sz-1; i>=0; i--){
16+
int cur_digit = num[i]-'0'; //assume all are number digits
17+
18+
if(should_double==1){
19+
cur_digit *= 2;
20+
}
21+
should_double = !should_double;
22+
if(cur_digit>=10){
23+
int tmp = cur_digit%10;
24+
tmp += (cur_digit/10)%10;
25+
cur_digit = tmp;
26+
}
27+
check_sum += cur_digit;
28+
}
29+
30+
return check_sum;
31+
}
32+
33+
int getFullAccountNumber(string number){
34+
int current_check_sum = helper(number);
35+
//cout<<"current checksum = "<<current_check_sum<<endl;
36+
int tmp = (10 - current_check_sum%10)%10;
37+
return tmp;
38+
}
39+
40+
41+
int main(){
42+
while(cin>>number){
43+
int ans = getFullAccountNumber(number);
44+
string whole_number = number;
45+
number += (ans + '0');
46+
cout<<"OUTPUT: "<<number<<endl;
47+
}
48+
return 0;
49+
}
50+

interviews/final_answer.cpp

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
string number;
6+
7+
8+
bool validNumber(string num){
9+
int sz = num.size();
10+
if(sz == 0)return true;
11+
for(int i=0; i<sz; i++){
12+
if(!(num[i]>='0' && num[i]<='9')){
13+
return false; //not valid
14+
}
15+
}
16+
return true;
17+
}
18+
19+
bool isValid(string num){
20+
if(!validNumber(num)){
21+
cout<<num<<" is not a valid number"<<endl;
22+
return false;
23+
}
24+
int sz = num.size();
25+
if(sz == 0)return true;
26+
27+
bool should_double = true;
28+
int check_sum = 0;
29+
30+
for(int i=sz-2; i>=0; i--){
31+
int cur_digit = num[i]-'0'; //assume all are number digits
32+
33+
if(should_double==1){
34+
cur_digit *= 2;
35+
}
36+
should_double = !should_double;
37+
if(cur_digit>=10){
38+
int tmp = cur_digit%10;
39+
tmp += (cur_digit/10)%10;
40+
cur_digit = tmp;
41+
}
42+
check_sum += cur_digit;
43+
}
44+
check_sum += num[sz-1]-'0';
45+
46+
if(check_sum % 10 == 0){
47+
return true;
48+
}else{
49+
return false;
50+
}
51+
}
52+
53+
int helper(string num){
54+
int sz = num.size();
55+
bool should_double = true;
56+
int check_sum = 0;
57+
for(int i=sz-1; i>=0; i--){
58+
int cur_digit = num[i]-'0'; //assume all are number digits
59+
if(should_double==1){
60+
cur_digit *= 2;
61+
}
62+
should_double = !should_double;
63+
if(cur_digit>=10){
64+
int tmp = cur_digit%10;
65+
tmp += (cur_digit/10)%10;
66+
cur_digit = tmp;
67+
}
68+
check_sum += cur_digit;
69+
}
70+
return check_sum;
71+
}
72+
73+
int getFullAccountNumber(string number){
74+
if(!validNumber(number)){
75+
return -1; // or throw an exception but I forget the syntax
76+
}
77+
78+
int current_check_sum = helper(number);
79+
int tmp = (10 - current_check_sum%10)%10;
80+
return tmp;
81+
}
82+
83+
84+
int main(){
85+
//call some function here
86+
87+
//test here
88+
//
89+
bool all_passed = true;
90+
91+
if(!isValid("")){
92+
all_passed = false;
93+
cout<<"didn't pass empty string"<<endl;
94+
}
95+
96+
if(!isValid("0")){
97+
all_passed = false;
98+
cout<<"didn't pass 0"<<endl;
99+
}
100+
101+
if(!isValid("00000000000000000000000000000000000000000000000")){
102+
all_passed = false;
103+
cout<<"didn't pass 00000000000000000000000000000000000000000000000"<<endl;
104+
}
105+
106+
if(isValid("01")){
107+
all_passed = false;
108+
cout<<"didn't pass 01"<<endl;
109+
}
110+
111+
if(isValid("&23")){
112+
all_passed = false;
113+
cout<<"didn't pass &23"<<endl;
114+
}
115+
116+
if(all_passed){
117+
cout<<"isValid() passed all testcases!"<<endl;
118+
}else{
119+
cout<<"DID NOT PASS TEST CASES"<<endl;
120+
}
121+
122+
if(isValid("123455")){
123+
cout<<"this is valid account number"<<endl;
124+
}
125+
126+
cout<<"12345 should append "<<getFullAccountNumber("12345")<<endl;
127+
128+
// run input validation
129+
130+
return 0;
131+
}
132+

interviews/test.in

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1111
2+
12345
3+
99
4+
999999999
5+
123455
6+
0

tc/srm562/a.out

-2.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)