/* Special notes in Picat. From https://theweeklychallenge.org/blog/perl-weekly-challenge-224/#TASK1 """ Task 1: Special Notes Submitted by: Mohammad S Anwar You are given two strings, $source and $target. Write a script to find out if using the characters (only once) from source, a target string can be created. Example 1 Input: $source = "abc" $target = "xyz" Output: false Example 2 Input: $source = "scriptinglanguage" $target = "perl" Output: true Example 3 Input: $source = "aabbcc" $target = "abc" Output: true """ [source = abc,target = xyz] not_ok [source = scriptinglanguage,target = perl] ok [source = aabbcc,target = abc] ok (For task 2 see additive_numbers.pi) This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go => Ss = [ ["abc","xyz"], ["scriptinglanguage","perl"], ["aabbcc","abc"] ], foreach([Source,Target] in Ss) println([source=Source,target=Target]), if check(Source,Target) then println(ok) else println(not_ok) end, nl end, nl. /* Some random runs source = zfrzzlbueuokkzvcodhyrpmrnctfxndytwxuhypoufyfevjsysqshejwgcbfsgdldbhkbwawdzdhwobxhtpqyangeqmwxpjcsrow target = npurqxamncjwvanvaccf not_ok source = hsugzlcdyfoivazlwsrkcnqlciovcbrltottbvybdplypnllfdxhqnuuyjpakhnfxgaydbbgqnefassfvqonfikdtzffislfalfe target = mhkeurjwjedgwrvbbfhv not_ok source = dyqzbnuwctqgzvpegxigyfjtavnvjsamttnwiislbktdiihohrwfwfawdoumiuydnlzyvujxgcaonjcwaacwieuluoxfjviyikwe target = ehdkmfbbodxqgbmogizc not_ok */ go2 => N = 100, Alpha = "abcdefghijklmnopqrstuvwxyz", Len = Alpha.len, _ = random2(), Source = [Alpha[random(1,Len)] : _ in 1..N], Target = [Alpha[random(1,Len)] : _ in 1..N div 5], println(source=Source), println(target=Target), if check(Source,Target) then println(ok) else println(not_ok) end, nl. check(Source,Target) => S = occurrences(Source), T = occurrences(Target), OK = true, foreach(C in T.keys, break(OK==false)) if S.get(C,0) < T.get(C) then OK := false end end, OK == true. % % occurrences(List): % returns a map with all the keys and the % number of occurrences of each elements. % occurrences(List) = Map => Map = new_map(), foreach(E in List) Map.put(E, Map.get(E,0)+1) end.