From b60cb77fe463f9c97a8170868a2ff874554d5f41 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 28 Nov 2023 11:53:44 +0000 Subject: - Added solutions by PokGoPun. - Added solutions by Luca Ferrari. - Added solutions by Humberto Massa. - Added solutions by Niels van Dijke. - Added solutions by Peter Campbell Smith. - Added solutions by Dave Jacoby. - Added solutions by Thomas Kohler. - Added solutions by Peter Meszaros. - Added solutions by E. Choroba. - Added solutions by David Ferrone. - Added solutions by W. Luis Mochan. - Added solutions by Mariano Spadaccini. --- challenge-245/clifton-wood/raku/ch-1.sh | 1 + challenge-245/clifton-wood/raku/ch-2.sh | 1 + challenge-245/eric-cheung/python/ch-1.py | 12 ++++++ challenge-245/eric-cheung/python/ch-2.py | 8 ++++ challenge-245/perlboy1967/perl/ch-1.pl | 39 +++++++++++++++++ challenge-245/perlboy1967/perl/ch-2.pl | 49 +++++++++++++++++++++ challenge-245/perlboy1967/perl/ch1.pl | 39 ----------------- challenge-245/perlboy1967/perl/ch2.pl | 49 --------------------- challenge-245/ulrich-rieke/cpp/ch-1.cpp | 47 ++++++++++++++++++++ challenge-245/ulrich-rieke/haskell/ch-1.hs | 15 +++++++ challenge-245/ulrich-rieke/haskell/ch-2.hs | 30 +++++++++++++ challenge-245/ulrich-rieke/perl/ch-1.pl | 20 +++++++++ challenge-245/ulrich-rieke/perl/ch-2.pl | 57 +++++++++++++++++++++++++ challenge-245/ulrich-rieke/raku/ch-1.raku | 15 +++++++ challenge-245/ulrich-rieke/raku/ch-2.raku | 55 ++++++++++++++++++++++++ challenge-245/ulrich-rieke/rust/ch-1.rs | 25 +++++++++++ challenge-245/ulrich-rieke/rust/ch-2.rs | 45 +++++++++++++++++++ challenge-245/ziameraj16/java/SortLanguage.java | 17 ++++++++ 18 files changed, 436 insertions(+), 88 deletions(-) create mode 100644 challenge-245/clifton-wood/raku/ch-1.sh create mode 100644 challenge-245/clifton-wood/raku/ch-2.sh create mode 100755 challenge-245/eric-cheung/python/ch-1.py create mode 100755 challenge-245/eric-cheung/python/ch-2.py create mode 100755 challenge-245/perlboy1967/perl/ch-1.pl create mode 100755 challenge-245/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-245/perlboy1967/perl/ch1.pl delete mode 100755 challenge-245/perlboy1967/perl/ch2.pl create mode 100755 challenge-245/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-245/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-245/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-245/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-245/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-245/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-245/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-245/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-245/ulrich-rieke/rust/ch-2.rs create mode 100644 challenge-245/ziameraj16/java/SortLanguage.java (limited to 'challenge-245') diff --git a/challenge-245/clifton-wood/raku/ch-1.sh b/challenge-245/clifton-wood/raku/ch-1.sh new file mode 100644 index 0000000000..377ecf4a92 --- /dev/null +++ b/challenge-245/clifton-wood/raku/ch-1.sh @@ -0,0 +1 @@ +raku -e 'my @lang = ("perl", "c", "python"); my @popularity = (2, 1, 3); sub sort-by-popularity { (@lang [Z] @popularity).sort( *.tail ).map( *.head ) }; sort-by-popularity.gist.say; @lang = ("c++", "haskell", "java"); @popularity = (1, 3, 2); sort-by-popularity.gist.say' diff --git a/challenge-245/clifton-wood/raku/ch-2.sh b/challenge-245/clifton-wood/raku/ch-2.sh new file mode 100644 index 0000000000..2d093899f6 --- /dev/null +++ b/challenge-245/clifton-wood/raku/ch-2.sh @@ -0,0 +1 @@ +raku -e 'sub TWC245_2 (@a) { @a.combinations.map( |*.permutations ).grep( +* ).map( |*.permutations ).map( *.join.Int ).sort( -* ).grep({ $_ % 3 == 0 }).head || -1 }; (8,9, 1).&TWC245_2.say; (8, 6, 7, 1, 0).&TWC245_2.say, @(1).&TWC245_2.say' diff --git a/challenge-245/eric-cheung/python/ch-1.py b/challenge-245/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..35addf394d --- /dev/null +++ b/challenge-245/eric-cheung/python/ch-1.py @@ -0,0 +1,12 @@ + +## Example 1 +## arrLang = ["perl", "c", "python"] +## arrPopularity = [2, 1, 3] + +## Example 2 +arrLang = ["c++", "haskell", "java"] +arrPopularity = [1, 3, 2] + +arrOutput = [arrLangLoop for arrPopularityLoop, arrLangLoop in sorted(zip(arrPopularity, arrLang))] + +print (arrOutput) diff --git a/challenge-245/eric-cheung/python/ch-2.py b/challenge-245/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..9f531e288b --- /dev/null +++ b/challenge-245/eric-cheung/python/ch-2.py @@ -0,0 +1,8 @@ + +## arrDigits = [8, 1, 9] ## Example 1 +arrDigits = [8, 6, 7, 1, 0] ## Example 2 + +if sum(arrDigits) % 3 == 0: + print ("".join(sorted([str(DigitLoop) for DigitLoop in arrDigits], reverse = True))) +else: + print (-1) diff --git a/challenge-245/perlboy1967/perl/ch-1.pl b/challenge-245/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..93878d5564 --- /dev/null +++ b/challenge-245/perlboy1967/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 245 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Sort Language +Submitted by: Mohammad S Anwar + +You are given two array of languages and its popularity. + +Write a script to sort the language based on popularity. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use List::MoreUtils qw(pairwise); + +sub sortLanguage (\@\@) { + map { $_->[0] } + sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } + pairwise { [$a,$b] } @{$_[0]},@{$_[1]}; +} + +is([sortLanguage(@{[qw{Perl C Python}]},@{[2,1,3]})], + [qw(C Perl Python)]); +is([sortLanguage(@{[qw{C++ Haskell Java}]},@{[1,3,2]})], + [qw(C++ Java Haskell)]); +is([sortLanguage(@{[qw{C Basic Assembly}]},@{[1,1,1]})], + [qw(Assembly Basic C)]); + +done_testing; diff --git a/challenge-245/perlboy1967/perl/ch-2.pl b/challenge-245/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..8c18d7fde4 --- /dev/null +++ b/challenge-245/perlboy1967/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 245 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Largest of Three +Submitted by: Mohammad S Anwar + +You are given an array of integers >= 0. + +Write a script to return the largest number formed by concatenating some of the given +integers in any order which is also multiple of 3. Return -1 if none found. + +=cut + +use v5.32; +use common::sense; + +use Test2::V0; + +use Algorithm::Combinatorics qw(permutations subsets); + +sub largestOfThree (@) { + my @l = sort { $b <=> $a } @_; + + my $subsetSize = $#l+1; + while ($subsetSize > 0) { + for my $subset (subsets(\@l,$subsetSize)) { + my @subset = sort { $b <=> $a } @$subset; + for my $p (permutations(\@subset)) { + my $s = join('',@$p); + return $s if ($s % 3 == 0); + } + $subsetSize--; + } + } + return -1; +} + + +is(largestOfThree(8,1,9),981); +is(largestOfThree(8,6,7,1,0),8760); +is(largestOfThree(1),-1); + +done_testing; diff --git a/challenge-245/perlboy1967/perl/ch1.pl b/challenge-245/perlboy1967/perl/ch1.pl deleted file mode 100755 index 93878d5564..0000000000 --- a/challenge-245/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 245 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 - -Author: Niels 'PerlBoy' van Dijke - -Task 1: Sort Language -Submitted by: Mohammad S Anwar - -You are given two array of languages and its popularity. - -Write a script to sort the language based on popularity. - -=cut - -use v5.32; -use common::sense; - -use Test2::V0; - -use List::MoreUtils qw(pairwise); - -sub sortLanguage (\@\@) { - map { $_->[0] } - sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } - pairwise { [$a,$b] } @{$_[0]},@{$_[1]}; -} - -is([sortLanguage(@{[qw{Perl C Python}]},@{[2,1,3]})], - [qw(C Perl Python)]); -is([sortLanguage(@{[qw{C++ Haskell Java}]},@{[1,3,2]})], - [qw(C++ Java Haskell)]); -is([sortLanguage(@{[qw{C Basic Assembly}]},@{[1,1,1]})], - [qw(Assembly Basic C)]); - -done_testing; diff --git a/challenge-245/perlboy1967/perl/ch2.pl b/challenge-245/perlboy1967/perl/ch2.pl deleted file mode 100755 index 8c18d7fde4..0000000000 --- a/challenge-245/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 245 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-245 - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Largest of Three -Submitted by: Mohammad S Anwar - -You are given an array of integers >= 0. - -Write a script to return the largest number formed by concatenating some of the given -integers in any order which is also multiple of 3. Return -1 if none found. - -=cut - -use v5.32; -use common::sense; - -use Test2::V0; - -use Algorithm::Combinatorics qw(permutations subsets); - -sub largestOfThree (@) { - my @l = sort { $b <=> $a } @_; - - my $subsetSize = $#l+1; - while ($subsetSize > 0) { - for my $subset (subsets(\@l,$subsetSize)) { - my @subset = sort { $b <=> $a } @$subset; - for my $p (permutations(\@subset)) { - my $s = join('',@$p); - return $s if ($s % 3 == 0); - } - $subsetSize--; - } - } - return -1; -} - - -is(largestOfThree(8,1,9),981); -is(largestOfThree(8,6,7,1,0),8760); -is(largestOfThree(1),-1); - -done_testing; diff --git a/challenge-245/ulrich-rieke/cpp/ch-1.cpp b/challenge-245/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..fb9584efcf --- /dev/null +++ b/challenge-245/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Enter some computer languages, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector languages( split( line , " " ) ) ; + std::cout << "Enter some popularity indices, separated by blanks!\n" ; + std::getline( std::cin , line ) ; + std::vector pops ( split( line , " " ) ) ; + std::vector popularities ; + for ( auto s : pops ) + popularities.push_back( std::stoi( s ) ) ; + int len = languages.size( ) ; + std::vector> pop_pairs ; + for ( int i = 0 ; i < len ; i++ ) { + pop_pairs.push_back( std::make_pair( popularities[ i ] , languages[ i ] ) ) ; + } + std::sort( pop_pairs.begin( ) , pop_pairs.end( ) , []( const auto & p1 , + const auto & p2 ) { return p1.first < p2.first ; } ) ; + std::cout << "(" ; + for ( const auto & p : pop_pairs ) { + std::cout << p.second ; + if ( p != pop_pairs.back( ) ) { + std::cout << "," ; + } + } + std::cout << ")\n" ; + return 0 ; +} diff --git a/challenge-245/ulrich-rieke/haskell/ch-1.hs b/challenge-245/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..76402219ac --- /dev/null +++ b/challenge-245/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,15 @@ +module Challenge245 + where +import Data.List ( sortOn ) ; + +solution :: [String] -> [Int] -> [String] +solution languages popularities = map fst $ sortOn snd $ zip languages popularities + +main :: IO ( ) +main = do + putStrLn "Enter some computer languages, separated by blanks!" + languages <- getLine + putStrLn "Enter some corresponding popularities, separated by blanks!" + popularities <- getLine + let pops = map read $ words popularities + print $ solution ( words languages ) pops diff --git a/challenge-245/ulrich-rieke/haskell/ch-2.hs b/challenge-245/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..85e9691ca8 --- /dev/null +++ b/challenge-245/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,30 @@ +module Challenge245_2 + where +import Data.List ( permutations , sort ) + +combinations :: Int -> [a] -> [[a]] +combinations 0 _ = [[]] +combinations n xs = [ xs !! i : x | i <- [0..(length xs ) - 1 ] , + x <- combinations (n - 1 ) ( drop ( i + 1 ) xs ) ] + +concatenate :: [Int] -> Int +concatenate list + |length list == 1 = head list + |otherwise = read $ foldl1 ( ++ ) $ map show list + +solution :: [Int] -> Int +solution list + |length sorted == 0 = -1 + |otherwise = last sorted + where + possiblePermus = map concatenate $ concat $ map permutations $ concat $ map + (\i -> combinations i list )[1..length list] + sorted = sort $ filter (\n -> mod n 3 == 0 ) possiblePermus + +main :: IO ( ) +main = do + putStrLn "Please enter some integers greater than 0 , separated by blanks!" + numberstrings <- getLine + let numbers = if length numberstrings == 1 then [read numberstrings] + else map read $ words numberstrings + print $ solution numbers diff --git a/challenge-245/ulrich-rieke/perl/ch-1.pl b/challenge-245/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..eacac696c7 --- /dev/null +++ b/challenge-245/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter some programming languages, separated by blanks!" ; +my $line = ; +chomp $line ; +my @languages = split( /\s/ , $line ) ; +say "Enter some popularity indices, separated by blanks!" ; +$line = ; +chomp $line ; +my @popularity = split( /\s/ , $line ) ; +my @pairs ; +for my $i ( 0..scalar( @languages) - 1 ) { + push @pairs , [$popularity[ $i ] , $languages[ $i ]] ; +} +my @sorted = sort { $a->[0] <=> $b->[0] } @pairs ; +print "(" ; +say join( ',' , map { $_->[1] } @sorted ) . ")" diff --git a/challenge-245/ulrich-rieke/perl/ch-2.pl b/challenge-245/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..e62ff04b4d --- /dev/null +++ b/challenge-245/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use Algorithm::Combinatorics qw ( permutations combinations ) ; + +sub concatenate { + my $numbers = shift ; + my $concat ; + map { $concat .= $_ } @$numbers ; + return $concat ; +} + +say "Enter some numbers greater than 0 , separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +my $iter = permutations( \@numbers ) ; +my @permuted ; +while ( my $c = $iter->next ) { + push @permuted , concatenate( $c ) ; +} +my @selected = grep { $_ % 3 == 0 } @permuted ; +if ( @selected ) { + my @sorted = sort { $b <=> $a } @selected ; + say $sorted[ 0 ] ; +} +else { + my $len = scalar( @numbers ) ; + my $i = $len ; + my @sorted ; + while ( $i != 1 ) { + @permuted = ( ) ; + $iter = combinations( \@numbers , $i ) ; + while ( my $c = $iter->next ) { + my @combi = @$c ; + my $permuiter = permutations( \@combi ) ; + while ( my $permuc = $permuiter->next ) { + push @permuted , concatenate( $permuc ) ; + } + } + @selected = grep { $_ % 3 == 0 } @permuted ; + if ( @selected ) { + @sorted = sort { $b <=> $a } @selected ; + last ; + } + else { + $i-- ; + } + } + if ( @selected ) { + say $sorted[0] ; + } + else { + say "-1" ; + } +} diff --git a/challenge-245/ulrich-rieke/raku/ch-1.raku b/challenge-245/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..2257e5dc20 --- /dev/null +++ b/challenge-245/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,15 @@ +use v6 ; + +say "Enter some computer languages, separated by blanks!" ; +my $line = $*IN.get ; +my @languages = $line.words ; +say "Enter some popularity indices for these languages!" ; +$line = $*IN.get ; +my @popularity = $line.words.map( {.Int} ) ; +my @pairs ; +for (0..@languages.elems - 1) -> $index { + @pairs.push( (@popularity[ $index ] , @languages[ $index ] ) ) ; +} +my @sorted = @pairs.sort( { $^a[0] cmp $^b[0] } ) ; +print "(" ; +say @sorted.map( {$_[1]} ).join( ',' ) ~ ")" ; diff --git a/challenge-245/ulrich-rieke/raku/ch-2.raku b/challenge-245/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..45b8866774 --- /dev/null +++ b/challenge-245/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,55 @@ +use v6 ; +#concatenate by gluing the digits together to a string and by converting +#them to an int afterwards +sub concatenate( $aSequence ) { + my $total ; + my $len = $aSequence.elems ; + for (0..$len - 1 ) -> $num { + $total ~= $aSequence[ $num ].Str ; + } + return $total.Int ; +} + +say "Enter some integers greater than 0 , separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my @permuted ; #for the concatenated permutations +my @permutations = @numbers.permutations( ) ; +for @permutations -> $permu { + @permuted.push( concatenate( $permu ) ) ; +} +my @selected = @permuted.grep( { $_ %% 3 } ) ; +if ( @selected.elems > 0 ) { + @selected .= sort ; #sort in ascending order + say @selected[*-1] ; #and select the largest one +} +else {#create combinations of decreasing length and permutate each +#combination. Stop when a number divisible by 3 has been found + my $len = @numbers.elems ; + my $i = $len ; + @permuted = ( ) ;#clear the array we've used before + while ( $i != 1 ) { + my @combinations = @numbers.combinations( $i ) ; + for @combinations -> $combi { + my @permu = $combi.permutations( ) ; + for @permu -> $aPermu { + my $concat = concatenate( $aPermu ) ; + @permuted.push( $concat ) ; + } + } + @selected = @permuted.grep( { $_ %% 3 } ) ; + if ( @selected ) { + @selected .= sort ; + last ; + } + else { + $i-- ; + } + } + if ( @selected ) { + say @selected[*-1] ; + } + else { + say "-1" ; + } +} diff --git a/challenge-245/ulrich-rieke/rust/ch-1.rs b/challenge-245/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..6fde09a21b --- /dev/null +++ b/challenge-245/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,25 @@ +use std::io ; + +fn main() { + println!("Enter some computer languages, separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let languages : Vec<&str> = entered_line.split_whitespace( ).map( | s | + s.trim( ) ).collect( ) ; + println!("Enter as many popularity indices, separated by blanks!") ; + let mut snd_line : String = String::new( ) ; + io::stdin( ).read_line( &mut snd_line ).unwrap( ) ; + let second_line : &str = &*snd_line ; + let popularity : Vec = second_line.split_whitespace( ).map( | s | + s.trim( ).parse::( ).unwrap( ) ).collect( ) ; + let mut contest : Vec<(u8 , &str)> = Vec::new( ) ; + let mut iter = popularity.iter( ).zip(languages.iter( ) ) ; + while let Some( ( &i , &lang ) ) = iter.next( ) { + contest.push( (i , lang ) ) ; + } + contest.sort_by( | a , b | a.0.cmp( &b.0 ) ) ; + print!("(" ) ; + contest.iter( ).for_each( | p | print!("{:?} " , p.1 ) ) ; + println!(")" ) ; +} diff --git a/challenge-245/ulrich-rieke/rust/ch-2.rs b/challenge-245/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..8db9155423 --- /dev/null +++ b/challenge-245/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,45 @@ +use std::io ; +use itertools::Itertools ; + +fn concatenate( nums : Vec<&u32> ) -> u32 { + let mut total : String = String::new( ) ; + for n in nums { + let str : String = (*n).to_string( ) ; + total.push_str( &str ) ; + } + let numberstr : &str = total.as_str( ) ; + numberstr.parse::().unwrap( ) +} + +fn main() { + println!("Enter some integers greater than 0 , separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::( ).unwrap( ) ).collect( ) ; + let len = numbers.len( ) ; + let mut selected : Vec = Vec::new( ) ; + let mut i : usize = len ; + while i != 1 { + let perms = numbers.iter( ).permutations( i ) ; + let mut permuted : Vec = Vec::new( ) ; + for vec in perms { + let concat : u32 = concatenate( vec ) ; + permuted.push( concat ) ; + } + permuted.iter( ).filter( | n | *n % 3 == 0 ).for_each( | num | + selected.push( *num ) ) ; + if selected.len( ) > 0 { + selected.sort_by( | a , b | b.cmp( a ) ) ; + break ; + } + i -= 1 ; + } + if selected.len( ) > 0 { + println!("{}" , selected[0] ) ; + } + else { + println!("-1") ; + } +} diff --git a/challenge-245/ziameraj16/java/SortLanguage.java b/challenge-245/ziameraj16/java/SortLanguage.java new file mode 100644 index 0000000000..510c860ec0 --- /dev/null +++ b/challenge-245/ziameraj16/java/SortLanguage.java @@ -0,0 +1,17 @@ +import java.util.*; + +public class SortLanguage { + + public static void main(String[] args) { + final Scanner scanner = new Scanner(System.in); + System.out.println("Enter comma separated languages"); + List languages = Arrays.stream(scanner.nextLine().split(",")).toList(); + System.out.println("Enter comma separated popularity value"); + List popularityList = Arrays.stream(scanner.nextLine().split(",")).map(Integer::valueOf).toList(); + List output = new ArrayList<>(popularityList.size()); + for (int i = 1; i <= popularityList.size(); i++) { + output.add(languages.get(popularityList.indexOf(i))); + } + System.out.println(output); + } +} -- cgit