Are they the "same"?
Description:
Given two arrays a
and b
write a function comp(a, b)
(orcompSame(a, b)
) that checks whether the two arrays have the "same" elements, with the same multiplicities (the multiplicity of a member is the number of times it appears). "Same" means, here, that the elements in b
are the elements in a
squared, regardless of the order.
Examples
Valid arrays
a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]
comp(a, b)
returns true because in b
121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b
's elements in terms of squares:
a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19]
Invalid arrays
If, for example, we change the first number to something else, comp
is not returning true anymore:
a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [132, 14641, 20736, 361, 25921, 361, 20736, 361]
comp(a,b)
returns false because in b
132 is not the square of any number of a
.
a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361]
comp(a,b)
returns false because in b
36100 is not the square of any number of a
.
Remarks
a
orb
might be[] or {}
(all languages except R, Shell).a
orb
might benil
ornull
orNone
ornothing
(except in C++, COBOL, Crystal, D, Dart, Elixir, Fortran, F#, Haskell, Nim, OCaml, Pascal, Perl, PowerShell, Prolog, PureScript, R, Racket, Rust, Shell, Swift).
If a
or b
are nil
(or null
or None
, depending on the language), the problem doesn't make sense so return false.
Stats:
Created | Mar 14, 2015 |
Published | Mar 14, 2015 |
Warriors Trained | 296067 |
Total Skips | 60318 |
Total Code Submissions | 1998472 |
Total Times Completed | 107541 |
Ruby Completions | 3756 |
C# Completions | 6195 |
Python Completions | 34254 |
Java Completions | 11576 |
Clojure Completions | 548 |
Haskell Completions | 1394 |
JavaScript Completions | 32127 |
CoffeeScript Completions | 53 |
Elixir Completions | 554 |
TypeScript Completions | 2640 |
C++ Completions | 4631 |
PHP Completions | 2248 |
Crystal Completions | 51 |
F# Completions | 181 |
C Completions | 2603 |
Rust Completions | 1703 |
Swift Completions | 886 |
R Completions | 247 |
Scala Completions | 760 |
Shell Completions | 128 |
OCaml Completions | 91 |
Julia Completions | 116 |
Kotlin Completions | 1015 |
PowerShell Completions | 78 |
Go Completions | 1422 |
Nim Completions | 37 |
PureScript Completions | 35 |
NASM Completions | 20 |
Racket Completions | 65 |
Groovy Completions | 45 |
Prolog Completions | 35 |
CFML Completions | 20 |
Fortran Completions | 20 |
Haxe Completions | 12 |
Dart Completions | 606 |
Pascal Completions | 22 |
Lua Completions | 91 |
Perl Completions | 43 |
D Completions | 17 |
Elm Completions | 13 |
COBOL Completions | 6 |
Erlang Completions | 19 |
Total Stars | 3994 |
% of votes with a positive feedback rating | 76% of 10563 |
Total "Very Satisfied" Votes | 6766 |
Total "Somewhat Satisfied" Votes | 2564 |
Total "Not Satisfied" Votes | 1233 |