Last active
January 29, 2019 04:30
-
-
Save ekopradesga/cf564ef75de8d0a73bbfb4130aba36b1 to your computer and use it in GitHub Desktop.
tantangan phpid for student telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class CheckPairEndSum { | |
public static void main(String args[]) { | |
if (args != null) { | |
for (String d : args) System.out.println(d); | |
} | |
if (args == null) { | |
System.out.println("return false 1"); | |
return; | |
} | |
if (args.length < 2) { | |
System.out.println("return false 2"); | |
return; | |
} | |
int _sum = Integer.parseInt(args[1]); | |
if (_sum % 2 != 0) { | |
System.out.println("return false 3"); | |
return; | |
} | |
String[] _arg1 = args[0].split(","); | |
List<String> _data = Arrays.asList(_arg1); | |
List<Integer> _pairs = new ArrayList(); | |
int _gsum = 0; | |
for(String s : _data) { | |
int cn = -1; | |
try { | |
cn = Integer.parseInt(s); | |
} catch(Exception e) {} | |
if (Collections.frequency(_data, s) == 2 && cn != -1 && Collections.frequency(_pairs, cn) == 0) { | |
_pairs.add(cn); | |
_gsum += cn * 2; | |
} | |
} | |
if (_pairs.size() < 0) { | |
System.out.println("return false 4"); | |
return; | |
} | |
if (_gsum != _sum) { | |
System.out.println("return false 5"); | |
return; | |
} | |
System.out.println("return true"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function checkPairEndSum($arr, $sum) { | |
if ($sum % 2 != 0) return; | |
if (!is_array($arr)) return; | |
$pairs = []; | |
$gsum = 0; | |
$carr = array_count_values($arr); | |
foreach ($carr as $ke => $ca) { | |
if ($ca == 2) { | |
$pairs[] = $ke; | |
$gsum += $ke * 2; | |
} | |
} | |
if (count($pairs) == 0) return; | |
if ($gsum !== $sum) return; | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while True: | |
try: | |
data = input("array : ") | |
data = [ int(i) for i in data.split() ] | |
break | |
except ValueError: | |
print("invalid array") | |
while True: | |
try: | |
_sum = input("sum : ") | |
_sum = int(_sum) | |
break | |
except ValueError: | |
print("invalid sum") | |
if _sum % 2 != 0: | |
print("return : false(1)") | |
else: | |
pairs = [] | |
acc = 0 | |
for n in data: | |
if data.count(n) == 2: | |
if pairs.count(n) == 0: | |
pairs.append(n) | |
acc += n * 2 | |
if len(pairs) == 0: | |
print("return : false(2)") | |
else: | |
print("return : true" if acc == _sum else "return : false(3)") | |
while True: | |
input("Press Enter to continue...") | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment