/* Twice largest in Picat. https://theweeklychallenge.org/blog/perl-weekly-challenge-191/#TASK1 """ Task 1: Twice Largest Submitted by: Mohammad S Anwar You are given list of integers, @list. Write a script to find out whether the largest item in the list is at least twice as large as each of the other items. Example 1 Input: @list = (1,2,3,4) Output: -1 The largest in the given list is 4. However 4 is not greater than twice of every remaining elements. 1 x 2 <= 4 2 x 2 <= 4 2 x 3 > 4 Example 2 Input: @list = (1,2,0,5) Output: 1 The largest in the given list is 5. Also 5 is greater than twice of every remaining elements. 1 x 2 <= 5 2 x 2 <= 5 0 x 2 <= 5 Example 3 Input: @list = (2,6,3,1) Output: 1 The largest in the given list is 6. Also 6 is greater than twice of every remaining elements. 2 x 2 <= 6 3 x 2 <= 6 1 x 2 <= 6 Example 4 Input: @list = (4,5,2,3) Output: -1 The largest in the given list is 5. Also 5 is not greater than twice of every remaining elements. 4 x 2 > 5 2 x 2 <= 5 3 x 2 > 5 """ Via http://www.rabbitfarm.com/cgi-bin/blosxom/2022/11/20 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import v3_utils. % import util. % import cp. main => go. go => Ls = [ [1,2,3,4], [1,2,0,5], [4,5,2,3] ], foreach(L in Ls) println(L), if L.twice_largest() then println(ok1) else println(not_ok1) end, if L.twice_largest2() then println(ok2) else println(not_ok2) end, nl end, nl. twice_largest(L) => S = L.sort_down(), S[1] >= S[2]*2. twice_largest2(L) => Max = max(L), select(Max,L,L2), Max2 = max(L2), Max >= Max2*2.