Cody

Problem 1547. Relative ratio of "1" in binary number

Solution 2763052

Submitted on 31 Jul 2020 by Ramesh Kumar V
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
x = 0; y_correct = 0; assert(isequal(ones_ratio(x),y_correct))

x = '0' m = 1 c = 0 r = 0

2   Pass
x = 1; y_correct = 1; assert(isequal(ones_ratio(x),y_correct))

x = '1' m = 1 c = 0 c = 1 r = 1

3   Pass
x = 2; y_correct = 0.5; assert(isequal(ones_ratio(x),y_correct))

x = '10' m = 2 c = 0 c = 1 r = 0.500000000000000

4   Pass
x = 7; y_correct = 1; assert(isequal(ones_ratio(x),y_correct))

x = '111' m = 3 c = 0 c = 1 c = 2 c = 3 r = 1

5   Pass
x = 128; y_correct = 0.125; assert(isequal(ones_ratio(x),y_correct))

x = '10000000' m = 8 c = 0 c = 1 r = 0.125000000000000

6   Pass
x = 1265476542; y_correct = 19/31; assert(isequal(ones_ratio(x),y_correct))

x = '1001011011011011010001110111110' m = 31 c = 0 c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 c = 7 c = 8 c = 9 c = 10 c = 11 c = 12 c = 13 c = 14 c = 15 c = 16 c = 17 c = 18 c = 19 r = 0.612903225806452

7   Pass
x = 98917653181; y_correct = 23/37; assert(isequal(ones_ratio(x),y_correct))

x = '1011100000111111100111001101010111101' m = 37 c = 0 c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 c = 7 c = 8 c = 9 c = 10 c = 11 c = 12 c = 13 c = 14 c = 15 c = 16 c = 17 c = 18 c = 19 c = 20 c = 21 c = 22 c = 23 r = 0.621621621621622

Suggested Problems

More from this Author9

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!