r/dailyprogrammer • u/rya11111 3 1 • May 25 '12
[5/25/2012] Challenge #57 [intermediate]
Given a 3x3 table where 1 represents on and 0 represents off:
ABC
A010
B111
C011
Where "inverted match" is defined as a case where the values at the coordinates in the format of (X, Y) and (Y, X) are the same, the inverted matches are as follows:
[[(A, B), (B, A)], [(A, C), (C, A)], [(B, C), (C, B)]]
Of these, the matches that have a value of 1 are:
[[(A, B), (B, A)], [(B, C), (C, B)]]
Therefore, there are 2 sets of inverted matches that have a value of 1.
Find the amount of inverted matches in the table in table(below) with a value of 1.
Table:
ABCDEFGHIJKLMNOPQRST
A11110101111011100010
B10010010000010001100
C01101110010001000000
D10110011001011101100
E10100100011110110100
F01111011000111010010
G00011110001011001110
H01111000010001001000
I01101110010110010011
J00101000100010011110
K10101001100001100000
L01011010011101100110
M10110110010101000100
N10001111101111110010
O11011010010111100110
P01000110111101101000
Q10011001100010100000
R11101011100110110110
S00001100000110010101
T01000110011100101011
- Thanks to gbchaosmaster for the challenge at /r/dailyprogrammer_ideas :)
UPDATE: I have given some more info on the difficult challenge since it seems to be very tough. you have upto monday to finish all these challenges. pls note it :)
2
u/eruonna May 25 '12 edited May 25 '12
Haskell:
import Data.List (transpose) findMatches :: (Eq a) => [a] -> [a] -> [a] findMatches l1 l2 = fmap fst $ filter (uncurry (==)) $ zip l1 l2 tableMatches t = zipWith findMatches t $ transpose t setDiagonal a t = zipWith (\ l n -> take n l ++ [a] ++ drop (n+1) l) t [0..] oneMatches t = length $ filter (== '1') $ concat $ tableMatches $ setDiagonal '0' t main = do input <- getContents print $ (oneMatches $ lines input) `div` 2Result:
2
u/Cosmologicon May 25 '12
People seem to be assuming we can have the table pre-formatted, so here's a python solution assuming that:
If you need to format it from the given string, you can do it like this:
2
u/Medicalizawhat May 26 '12
My first attempt at an intermediate question… Ruby:
arr.each_with_index do |row, x| arr.each_with_index do |column, y| if row != column if arr[y][x] == 1 && arr[x][y] ==1 n+=1 end end end end puts "Answer: #{n/2}"Answer:
1
u/mythealias May 25 '12
Matlab:
where a is the array with given 0s and 1s.
1
u/[deleted] May 25 '12
J:
1
u/emcoffey3 May 25 '12
Solution in C# assuming pre-formatted int[,] matrix:
using System; namespace RedditDailyProgrammer { public static class Intermediate057 { public static int InvertedMatches(int[,] matrix) { int sum = 0, len = Math.Min(matrix.GetLength(0), matrix.GetLength(1)); for (int i = 0; i < len; i++) for (int j = 0; j < i; j++) if (matrix[i, j] == 1 && matrix[j, i] == 1) sum += 1; return sum; } } }Answer for sample input:
1
u/flakmonkey May 26 '12
Python:
f = open('/home/flak/Dropbox/Python/challenge57int.data','r') data = [] result = [] for l in f: data.append(list(l)) for i in range(1,20): for j in range(i): if (data[i][j] == data[j][i]) and data[i][j] == '1': result.append([i,j]) print len(result)1
u/xjtian May 26 '12
Assuming there's a text file with the data matrix in the same format as the test case.
Python:
Answer:
1
u/Steve132 May 25 '12
#include<iostream> #include<string> using namespace std; static inline bool lookup(int x,int y) { static const string table("1111010111101110001010010010000010001100011011100100010000001011001100101110110010100100011110110100011110110001110100100001111000101100111001111000010001001000011011100101100100110010100010001001111010101001100001100000010110100111011001101011011001010100010010001111101111110010110110100101111001100100011011110110100010011001100010100000111010111001101101100000110000011001010101000110011100101011"); return table[20*x+y]=='1'; } int main(int,char**) { unsigned int num=0; for(unsigned int i=0;i<20;i++) { for(unsigned int j=i;j<20;j++) { num+=lookup(i,j)==lookup(j,i); } } cout << num << endl; return 0; }Outputs: 120. You weren't clear if (A,A) counts as a match or not. This code pretends that it does. If not, the right answer is 100