Put below two lines into ~/.bash_profile
export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8
In terminal, run the below command (or restart the user session)
source ~/.bash_profile
Put below two lines into ~/.bash_profile
export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8
In terminal, run the below command (or restart the user session)
source ~/.bash_profile
Pre-requisite: Working Apache Server.
In httpd-vhosts.conf:
Add below content inside <VirtualHost …>
ScriptAlias /cgi-bin/ "/cgi-bin/" <directory "<httpd-installed-path="">/cgi-bin/"> Options Indexes FollowSymLinks ExecCGI AddHandler cgi-script .cgi .py AllowOverride None Require all granted
Now create a new file ‘test.py’ in /cgi-bin/ with content:
#!/usr/bin/env python import cgi cgi.test()
Make sure the below line is not commented in httpd.conf
LoadModule cgi_module modules/mod_cgi.so
Start/Restart apache server.
You can verify the loaded cgi module using the command:
sudo [path/]apachectl -M | grep cgi
Try below url in web-browser:
http://<ip/hostname>:/cgi-bin/test.py eg: http://localhost:1025/cgi-bin/test.py http://localhost:80/cgi-bin/test.py
You will get a page with details on current working directory, command line arguments, etc..
ERROR:
Modules/constants.c: In function ‘LDAPinit_constants’:
Modules/constants.c:158: error: ‘LDAP_OPT_DIAGNOSTIC_MESSAGE’ undeclared (first use in this function)
Modules/constants.c:158: error: (Each undeclared identifier is reported only once
Modules/constants.c:158: error: for each function it appears in.)
Modules/constants.c:380: error: ‘LDAP_CONTROL_RELAX’ undeclared (first use in this function)
error: command ‘gcc’ failed with exit status 1
Solution:
Install openldap24-libs & openldap24-libs-devel :
sudo yum install openldap24-libs-devel
sudo yum install openldap24-libs
Run the below commands and get the unique list of directories from the output:
Add those folders to setup.cfg file in the below section:
[_ldap]
library_dirs = /opt/openldap-RE24/lib /usr/lib
include_dirs = /opt/openldap-RE24/include /usr/include/sasl /usr/include
Now run the installation command:
python setup.py install
NOTE: Below code is Python 3.
base = 26 def transform(row): res = [] for field in [tmp.strip() for tmp in row.split(',')]: ival = 0 power = 0 for c in field[::-1]: ival += pow(base,power)*(ord(c)-ord('A')+1) power += 1 res.append(ival) return res print(transform("A, B, Z, AA, AB, AAA"))
Output:
[1, 2, 26, 27, 28, 703]
Problem
You receive a credit C
at a local store and would like to buy two items. You first walk through the store and create a list L
of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).
Input
The first line of input gives the number of cases, N. N test cases follow. For each test case there will be:
Output
For each test case, output one line containing “Case #x: ” followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.
Limits
5 ≤ C ≤ 1000
1 ≤ P ≤ 1000
Small dataset
N = 10
3 ≤ I ≤ 100
Large dataset
N = 50
3 ≤ I ≤ 2000
Sample
Input | Output |
3 |
Case #1: 2 3 |
C++ Solution:
/* Author : Sreejith Sreekantan Description : https://code.google.com/codejam/contest/351101/dashboard#s=p0 */ #include #include #include #include using namespace std; int main(int argc, char const *argv[]) { int numOfTestInstances; cin >> numOfTestInstances; std::vector itemWeight; for (int testInstanceNum = 0; testInstanceNum < numOfTestInstances; ++testInstanceNum) { int limit; cin >> limit; int numOfItems; cin >> numOfItems; itemWeight.clear(); itemWeight.reserve(numOfItems); for (int itemNum = 0; itemNum < numOfItems; ++itemNum) { cin >> itemWeight[itemNum]; } cout << "Case #" << testInstanceNum + 1 << ": "; for (int i = 0; i < numOfItems; ++i) { for (int j = i + 1; j < numOfItems; ++j) { if (itemWeight[i] > limit) { break; } if (itemWeight[j] > limit) { continue; } if (itemWeight[i] + itemWeight[j] == limit) { if (i < j) { cout << i + 1 << " " << j + 1 << endl; } else { cout << j + 1 << " " << i + 1 << endl; } } } } } return 0; }
Python Solution:
#! /usr/bin/python numOfInstances = int(raw_input()) for x in xrange(1,numOfInstances+1): limit = int(raw_input()) numOfItems = int(raw_input()) d = dict() items = [int(x) for x in raw_input().split()] for y in xrange(1,numOfItems+1): if items[y-1] not in d: d[items[y-1]] = set() d[items[y-1]].add(y) print d for x in d.keys(): if x <= limit: index = d[x].pop() if (limit-x) in d and len(d[limit-x])>0: index2=d[limit-x].pop() d[limit-x].add(index2) print min(index, index2), max(index, index2) d[x].add(index)
Problem
Given a list of space separated words, reverse the order of the words. Each line of text contains L
letters and W
words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.
Input
The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.
Output
For each test case, output one line containing “Case #x: ” followed by the list of words in reverse order.
Limits
Small dataset
N = 5
1 ≤ L ≤ 25
Large dataset
N = 100
1 ≤ L ≤ 1000
Sample
Input | Output |
3 |
Case #1: test a is this |
C++ Solution:
/* Author : Sreejith Sreekantan Description : Problem B. Reverse Words https://code.google.com/codejam/contest/351101/dashboard#s=p1 */ #include #include #include #include #include #include using namespace std; int main(int argc, char const *argv[]) { int numOfTestInstances; cin >> numOfTestInstances; for (int testInstanceNum = 0; testInstanceNum < numOfTestInstances; ++testInstanceNum) { istringstream in; string s; cin >> ws; getline(cin ,s); replace(s.begin(), s.end(), ' ', '\n'); in.str(s); stack stack_s_rev; while (in >> s) { stack_s_rev.push(s); } cout << "case #" << testInstanceNum+1 << ": "; while(!stack_s_rev.empty()) { cout << stack_s_rev.top() << " "; stack_s_rev.pop(); } cout << endl; } return 0; }
Problem
After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.
Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.
A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.
Input
The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.
Output
For each test case, output
Case #X: K
where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.
Limits
Small dataset
1 ≤ L ≤ 10
1 ≤ D ≤ 25
1 ≤ N ≤ 10
Large dataset
1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500
Sample
Input | Output |
3 5 4 |
Case #1: 2 |
C++ Solution:
/* Author : Sreejith Sreekantan Description : Problem A. Alien Language (https://code.google.com/codejam/contest/90101/dashboard#s=p0) */ #include #include #include #include #include #include #include #include using namespace std; #define rep(v,N) for(int v=0; v<n; v++)="" #define="" <span="" class="hiddenSpellError" pre="define " data-mce-bogus="1">rep2(v,M,N) for(int v=M; v<n; v++)="" #define="" for(v,c)="" for(__typeof(c.begin())="" v="C.begin();" v!="C.end();" ++v)="" vi="" std::vector<int=""> #define vll std::vector #define pb push_back #define Sort(C) std::sort(C.begin(), C.end()) #define RSort(C) std::sort(C.rbegin(), C.rend()) #define Copy(ans,out) copy(ans.begin(),ans.end(), ostream_iterator<__typeof(ans[0])>(out, " ")) // #define SMALL #define LARGE int main(int argc, char const *argv[]) { freopen("a.in", "rt", stdin); // freopen("a.out", "wt", stdout); #ifdef SMALL freopen("as.in", "rt", stdin); freopen("as.out", "wt", stdout); #endif #ifdef LARGE freopen("al.in", "rt", stdin); freopen("al.out", "wt", stdout); #endif unsigned int L, D, N; cin >> L >> D >> N; std::vector dict(D); std::vector inp(D); rep(i, D) { cin >> dict[i]; } rep(i, N) { cin >> inp[i]; replace(inp[i].begin(), inp[i].end(), '(', '['); replace(inp[i].begin(), inp[i].end(), ')', ']'); // cout << inp[i]<< endl; int c = 0; rep(j, D) { if (regex_match(dict[j], regex(inp[i])) ) c++; } cout << "Case #" << i+1 << ": " << c << endl; } return 0; }
T : Number of test cases
M: value of M
N: Number of strings
For each string, ascii value of each character of the string is raised to M and multiplied together. For each test case the sum of above values is ODD or EVEN
Input:
1
10 2
ac ab
Output:
ODD
TIP: Challenge lies in finding the solution without doing all the mathematical operations mentioned in the question. (yes, also called optimizing..!)
Solution:
#!/usr/bin/python T = int(raw_input()) while T>0: T = T - 1 tmp = raw_input() M = int(tmp.split()[0]) K = int(tmp.split()[1]) tmp = raw_input() final_odd = False for s in tmp.split(): odd = None for i in s: if ord(i)%2==0: # the power is even odd = False else: # the power is odd if odd==None: # if odd is True, odd*odd=odd odd = True # odd + odd = even # odd + even = odd # even + even = even if final_odd != odd: final_odd = True else: final_odd = False if final_odd: print "ODD" else: print "EVEN"
When you use re.findall(“(.*):” to get the strings ending with full-colon(:), you may not always get the expected result if your string has multiple full-colons. Lets run the command before we talk.
Concentrate on the commands put inside single colon. (You can neglect the rest as it is part of my effort to avoid creating a python file for running this code)
What happened here is python did a greedy search and found maximum string ending with a full-colon. If that is what you desired, stop reading further.. 🙂
If you expected a list of all the strings ending with full-colon, change your command to re.findall(“(.*?):”
As you can see, I added a question mark which will force python to avoid greedy approach.
Before we part, lets make sure python is ok with this change..
Output:
Question:
Find the biggest black square from an N*N matrix of squares. 1 means it’s black, 0 means it’s white. Output should be the list of squares which forms the largest black square.
Input : ‘{0#1#1#1#0#1#0#1,1#0#1#0#0#0#0#1,0#0#0#1#0#1#0#0,1#1#1#1#1#0#0#1,1#1#1#1#0#1#1#1,1#1#1#1#0#1#1#1,1#1#1#1#1#1#1#1,1#1#0#1#0#0#1#1}’
Output : {(3#0,3#1,3#2,3#3),(4#0,4#1,4#2,4#3),(5#0,5#1,5#2,5#3),(6#0,6#1,6#2,6#3)}
Solution(Python):
# Author : Sreejith Sreekantan # Description : # Find the biggest black square from an N*N matrix of squares. 1 means it's black, 0 means it's white # Input : '{0#1#1#1#0#1#0#1,1#0#1#0#0#0#0#1,0#0#0#1#0#1#0#0,1#1#1#1#1#0#0#1,1#1#1#1#0#1#1#1,1#1#1#1#0#1#1#1,1#1#1#1#1#1#1#1,1#1#0#1#0#0#1#1}' # Output : {(3#0,3#1,3#2,3#3),(4#0,4#1,4#2,4#3),(5#0,5#1,5#2,5#3),(6#0,6#1,6#2,6#3)} #!/usr/bin/python def largestSquareAt(sq, i, j, k=1): if not ( i+k<len(sq) and="" j+k<len(sq[i])="" ):="" ="" ="" return="" 0="" for="" x="" in="" range(0,k):="" if="" sq[i+x][j+k-1]="='0':" y="" sq[i+k-1][j+y]="='0':" 1="" +="" largestsquareat(sq,="" i,="" j,="" k+1) ="" ="" def="" biggestsquare(input1):="" len(input1)="=0" or="" not="" (="" input1.count('{')="=input1.count('}')" )="" :="" ''="" i="str(input1)" j="i" for="" j]="" resx="-1" rexy="-1" ressize="-1" res="[]" range(0,len(j)):="" res.append([])="" range(0,len(j[x])):="" res[x].append(largestsquareat(j,="" x,="" y))="" ressize<res[x][y]:="" resy="y" resstring="" ressize="">0: resstring = "{" for x in range(resx,resx+ressize): if x>resx: resstring += "," resstring += "(" for y in range(resy,resy+ressize): if y>resy: resstring += "," resstring += (str(x)+"#"+str(y)) resstring += ")" resstring += "}" return resstring input = '{0#1#1#0,0#1#1#1,0#1#1#1,1#1#1#1}' input1 = '{0#1#1#1#0#1#0#1,1#0#1#0#0#0#0#1,0#0#0#1#0#1#0#0,1#1#1#1#1#0#0#1,1#1#1#1#0#1#1#1,1#1#1#1#0#1#1#1,1#1#1#1#1#1#1#1,1#1#0#1#0#0#1#1}' input2 = '{0#1#1#1#0#1#0#1,1#0#1#0#0#0#0#1,0#0#0#1#0#1#0#0,1#1#1#1#1#0#0#1,1#1#0#1#0#1#1#1,1#1#1#1#0#1#1#1,1#1#1#1#1#1#1#1,1#1#0#1#0#0#1#1}' input3= '{0#1#1#1#0#1#0#1,1#0#1#0#0#0#0#1,0#0#0#1#0#1#0#0,1#1#1#1#1#0#0#1,0#1#0#1#0#1#1#1,1#1#1#1#0#1#1#1,1#1#1#1#1#1#1#1,1#1#0#1#0#0#1#1}' input4 = '{0#0#0#0,0#0#0#0,0#0#0#0,0#0#0#0}' print biggestSquare('') print biggestSquare(input1) print biggestSquare(input2) print biggestSquare(input3) print biggestSquare(input4)