## Questions d'entretien

Entretien pour Contract Ruby On Rails Software Engineer

-Mountain View, CA

# Describe a routine which returns the set of integers in {1..100} divisible without remainder by 3 but not by 9.

Répondre

## Réponses aux questions d'entretien

12 réponse(s)

6

(1..100).select { |x| x%3 == 0 && x%9 != 0

Matt le

4

1) start from number = 3 Loop while(number <= 100) 2) display number 3) number = number+3, display number 4) number = number+6 Loop

Vicky le

3

put those integers into an array, pick every third element, out of which discard every third element.

guest le

2

I'm assuming the question wants us to find integers that are divisible by 3 but not by 9. This can be easily obtained using a mod function inside the following if statement: if(number % 3 == 0 && number % 9 != 0) Here is a short program I wrote in c++ to show how to solve this problem. Instead of returning the set of integer, I just printed them out on the screen: #include #include using namespace std; int main(int argc, char** argv) { int i = 0; vector list; vector::iterator it; for(i = 1; i <= 100; i++) { if(i%3 == 0 && i%9 != 0) { list.push_back(i); } } for(it = list.begin(); it != list.end(); it++) { cout << *it << endl; } return 0; } If I missed anything, please let me know. Happy coding and problem solving!

Tyler le

2

A variation on Matt's answer: (1..100).select { |n| n % 3 == 0 }.reject { |n| n % 9 == 0 }

Sharon le

1

(1..100).map { |i| (i % 3).zero? && !(i % 9).zero? ? i : nil }.compact

Chris le

0

That'll certainly work, Tyler, but the OP indicated he was interviewing for a Ruby On Rails - not C++ - gig.

Anon le

0

The requirement doesn't say if the input has to be a Range. If it doesn't have to be, then we don't need to traverse each element but to simply calculate it. def get_nums_by_3_not_by_9(max) arr = [] x = max.to_i / 3 x.times do |i| next if i % 3 == 0 arr << i * 3 end return arr end

cel le

0

(1..100).select do |n| n%3 ==0 and n%9 != 0 end

Utilisateur anonyme le

0

(1..100).to_a.delete_if{|x| !(x%3==0 && x%9>0)} or (1..100).to_a.select{|x| x%3==0 && x%9>0} or (1..100).to_a.map{|x| x%3==0 && x%9>0 ? x : nil}.compact or (1..100).to_a.reject{|x| !(x%3==0 && x%9>0)}

Mihail le

1

python [x for x in range(0,100) if x % 3 == 0 and x % 9 != 0]

Tom le

0