Mishra.Anurag
BAN USERIn Ruby, heavily relies on sets. Doesn't add up any rows, columns, or subsquares. It checks if each row, column and subsquare is same as the set
<1,2,3,4,5,6,7,8,9>
require 'set'
class SudokuValidator
def initialize(board)
@board = board
@all_numbers = (1..9).to_set
end
def valid?()
return (1..9).all? do |n|
subsquare_valid?(n) and row_valid?(n) and column_valid?(n)
end
end
# check if nth sub-square contains 1..9
def subsquare_valid?(nth)
subsquare(nth) == @all_numbers
end
# check if nth row is valid
def row_valid?(nth)
row(nth) == @all_numbers
end
# check if nth column is valid
def column_valid?(nth)
column(nth) == @all_numbers
end
# return the nth subsquare as an array
def subsquare(nth)
start_row = ((nth - 1)/ 3) * 3
end_row = start_row + 2
start_col = (nth - 1) % 3
end_col = start_col + 2
@board[start_row..end_row].map { |row| row[start_col..end_col] }.flatten.to_set
end
# return the nth row as an array
def row(nth)
@board[nth - 1]
end
# return the nth column as an array
def column(nth)
@board.transpose[nth - 1]
end
end
This may not be the what the interviewer is looking for, but I cannot imagine modding for each digit and doing micro-optimizations here. This question needs to be deprecated - it's so 60s.
var n = 234;
n.toString().split('').join(',');
Use libraries :P
In Python
os.path.normpath("a/b/../d") -> "a/d"
it is irrelevant if the number occurs once or any odd number of times (3, 5, 7, ..). The important thing is that there is only one such number that occurs an odd number of times. When XOR'ing the numbers, all even number will have a pair which will cancel it out. It doesn't matter how many times that even number occurs as the important thing is that all of them can be paired and thus canceled out.
For odd numbers the same thing applies. There will always be one lone element that cannot be paired with anything else. If it occurs 1 time, that is our element. If it occurs 3 or more times, then there will be 2n pairs that cancel out, and the 1 remaining element will be in our answer.
Read up more about XOR and commutativity and associativity at Wikipedia if you still have doubts.
A good answer was posted by an Anonymous user on June 2. Copy the rotated string to itself, and check if this new string contains the target string. Also, need to check if the length of the rotated and the target string are exactly the same.
input: rotated, str
(rotated+rotated).contains(str)
See this StackOverflow.com for a bunch of different answers to this question - stackoverflow.com/questions/2553522/interview-question-check-if-one-string-is-a-rotation-of-other-string/2616295#2616295
This algorithm is O(n)
Does the path have to necessarily start at the root node and end at a leaf? In your original example, what if the desired sum was was 9, and there are connected nodes (3, 6) that sum to to that but neither starts at the root.
Also, a path could comprise of just a single node. Is the desired sum was 7, there are three paths that sum up to this value - (1, 2, 4), (2, 5), and (7).
A good model is probably not possible with object-oriented modeling.
- Mishra.Anurag June 10, 2010It's a way to query DOM nodes. Although it's mainly used in CSS, it's not limited to CSS anymore. Frameworks like jQuery, and MooTools make heavy use of these selectors in JavaScript.
See the latest specs (http://www.w3.org/TR/css3-selectors/) for more information.
Deleted the previous comment, but it does not seem to go away for a reason.
This is in Ruby 1.9.1, exploiting string ranges
def column(number)
n, t = number, 0
until n == 0; n /= 26; t += 1; end
("a".."z"*t).first(number).last
end
Not the most efficient solution, but it works :)
- Mishra.Anurag June 10, 2010+1 this works great
- Mishra.Anurag June 09, 2010
Editing doesn't work here.. repasting:
- Mishra.Anurag June 14, 2010