ArrayMac > Function display > TEST_ram_160602_Dyalog2016P

     ∇ r←TEST_ram_160602_Dyalog2016P;ckFnExists;unless;aOk;aEq;on;chkFeature;ckObExists;chkFeatureLine;tfuuN;tfuu;fThis;ms5;ms1;ms2;urlV;mean;ms3;ms4;ms6;ms7;ms8;ms9;msA                                                                                                                                                                                         
[1]   ⍝∇ 20160602/20160614 ra⌈ --()-- test "ram_160602_Dyalog2016P" 2015.0825                                                                                                                                                                                                                                                                                     
[2]    test_start                                                                                                                                                                                                                                                                                                                                                 
[3]    r←0                                                                                                                                                                                                                                                                                                                                                        
[4]    unless←{(~⍵)/⍺}                                                                                                                                                                                                                                                                                                                                            
[5]    aOk←#.Assert                                                                                                                                                                                                                                                                                                                                               
[6]    aEq←#.AssertEq                                                                                                                                                                                                                                                                                                                                             
[7]    on←{⍺ ⍵}                                                                                                                                                                                                                                                                                                                                                   
[8]   ⍝⍝--------                                                                                                                                                                                                                                                                                                                                                  
[9]    ckObExists←{(⍵ omrepl'missing "⍵"')AssertEq ⍺ on ⍬⍴⎕NC ⍵}                                                                                                                                                                                                                                                                                                  
[10]   ckFnExists←3∘ckObExists                                                                                                                                                                                                                                                                                                                                    
[11]   chkFeature←{⍺←0 ⋄ ('Requirement:',⍵)aEq 1 ⍺}                                                                                                                                                                                                                                                                                                               
[12]   chkFeatureLine←{⍺←0 ⋄ ⍺ chkFeature'⍝⍝--'{(¯1+(⍬⍴⍴⍺)+⍬⍴where ⍺⍷⍵)↓⍵}(1 atpars 2⌷[1]qSI'')FNline ⍵}                                                                                                                                                                                                                                                          
[13]   ⍝⍝--                                                                                                                                                                                                                                                                                                                                                       
[14]   urlV←'http://www.dyalogaplcompetition.com/'                                                                                                                                                                                                                                                                                                                
[15]   ⍝ urlV,←'?utm_source=email&utm_medium=email&utm_term=SC_listing&utm_campaign=Dyalog_2016'                                                                                                                                                                                                                                                                  
[16]   tfuuN←'ram_160602_Dyalog2016P'                                                                                                                                                                                                                                                                                                                             
[17]   :If 0                                                                                                                                                                                                                                                                                                                                                      
[18]       :If ~ckFnExists{⍵}tfuuN                                                                                                                                                                                                                                                                                                                                
[19]           ⎕←APLPmtMFS tfuuN                                                                                                                                                                                                                                                                                                                                  
[20]           ⎕←0 APLPmt ThisFN''                                                                                                                                                                                                                                                                                                                                
[21]           →XIT                                                                                                                                                                                                                                                                                                                                               
[22]       :EndIf                                                                                                                                                                                                                                                                                                                                                 
[23]       tfuu←⍎tfuuN                                                                                                                                                                                                                                                                                                                                            
[24]   :EndIf                                                                                                                                                                                                                                                                                                                                                     
[25]   fThis←{({⍵,'.'if×⍬⍴⍴⍵}2↓⍕⎕THIS),⍵}                                                                                                                                                                                                                                                                                                                         
[26]   ⍝⍝--                                                                                                                                                                                                                                                                                                                                                       
[27]   urlV←'https://tc.dyalogaplcompetition.com/#case-question-sets/c11cbc10c3540133345922000b4110c4/show'                                                                                                                                                                                                                                                       
[28]   ⎕←APLPmt'urlV←'urlV                                                                                                                                                                                                                                                                                                                                        
[29]  ⍝⍝..                                                                                                                                                                                                                                                                                                                                                        
[30]   ms1←{(+/⍵)÷{⍵+0∊⍵}⍬⍴⍴,⍵}                                                                                                                                                                                                                                                                                                                                   
[31]   →fail unless aEq 3.5 on ms1 1 2 3 4 5 6                                                                                                                                                                                                                                                                                                                    
[32]   →fail unless aEq 0 on ms1 ⍬ ⍝ the average of an empty vector is 0                                                                                                                                                                                                                                                                                          
[33]   →fail unless aEq 17 on ms1 17 ⍝ your solution should work with a scalar argument                                                                                                                                                                                                                                                                           
[34]                                                                                                                                                                                                                                                                                                                                                              
[35]   →fail unless aEq 6 on ms1 2 10                                                                                                                                                                                                                                                                                                                             
[36]                                                                                                                                                                                                                                                                                                                                                              
[37]   →fail unless 1 chkFeature'Solve Problem 1'                                                                                                                                                                                                                                                                                                                 
[38]                                                                                                                                                                                                                                                                                                                                                              
[39]   mean←ms1                                                                                                                                                                                                                                                                                                                                                   
[40]                                                                                                                                                                                                                                                                                                                                                              
[41]   ms5←{⍵~⍵/⍨(⍵⍳⍵)≠⍳⍬⍴⍴⍵}                                                                                                                                                                                                                                                                                                                                     
[42]   →fail unless aEq 1 2 3 4 5 on ms5 1 2 3 4 5                                                                                                                                                                                                                                                                                                                
[43]   →fail unless aEq(,5)on ms5 1 2 3 4 5 4 3 2 1                                                                                                                                                                                                                                                                                                               
[44]   →fail unless aEq'he wrd'on ms5'hello world'                                                                                                                                                                                                                                                                                                                
[45]   →fail unless 1 chkFeature'Solve Problem 5'                                                                                                                                                                                                                                                                                                                 
[46]                                                                                                                                                                                                                                                                                                                                                              
[47]   ms2←{0∊⍴⍵:0 ⋄ len←⌈0.5×⍬⍴⍴t←,⍵ ⋄ mean{({⍵}/len↑⍵),{⍺}/(-len)↑⍵}t[⍋t]}                                                                                                                                                                                                                                                                                      
[48]   →fail unless aEq(5)on ms2 1 2 3 4 5 6 7 8 9                                                                                                                                                                                                                                                                                                                
[49]   →fail unless aEq(4.5)on ms2 1 8 2 7 3 6 4 5                                                                                                                                                                                                                                                                                                                
[50]   →fail unless aEq(0)on ms2 ⍬                                                                                                                                                                                                                                                                                                                                
[51]   →fail unless aEq(7)on ms2 7                                                                                                                                                                                                                                                                                                                                
[52]   →fail unless 1 chkFeature'Solve Problem 2'                                                                                                                                                                                                                                                                                                                 
[53]                                                                                                                                                                                                                                                                                                                                                              
[54]  ⍝Problem 3 – Statistics – Mode                                                                                                                                                                                                                                                                                                                              
[55]  
[56]  ⍝Write a dfn that takes a numeric vector or scalar as its right argument and returns the mode (that is,                                                                                                                                                                                                                                                     
[57]  ⍝the most common value) of the array. If more than one number occurs the greatest number of times, return                                                                                                                                                                                                                                                   
[58]  ⍝all such numbers.                                                                                                                                                                                                                                                                                                                                          
[59]                                                                                                                                                                                                                                                                                                                                                              
[60]   ms3←{{(⍵[;2]=⌈/⍵[;2])/⍵[;1]}{⍺(⍬⍴⍴⍵)}quadEq ⍵}                                                                                                                                                                                                                                                                                                             
[61]  ⍝Test cases:                                                                                                                                                                                                                                                                                                                                                
[62]  
[63]  ⍝       {your_solution} 2 1 4 3 2 5 1 2                                                                                                                                                                                                                                                                                                                     
[64]  ⍝2                                                                                                                                                                                                                                                                                                                                                          
[65]   →fail unless aEq(,2)on ms3 2 1 4 3 2 5 1 2                                                                                                                                                                                                                                                                                                                 
[66]  ⍝       {your_solution} ⍬ ⍝ should return an empty vector                                                                                                                                                                                                                                                                                                   
[67]   →fail unless aEq ⍬ on ms3 ⍬                                                                                                                                                                                                                                                                                                                                
[68]  ⍝       {your_solution} 1 2 3 4 1 2                                                                                                                                                                                                                                                                                                                         
[69]  ⍝1 2                                                                                                                                                                                                                                                                                                                                                        
[70]   →fail unless aEq 1 2 on ms3 1 2 3 4 1 2                                                                                                                                                                                                                                                                                                                    
[71]                                                                                                                                                                                                                                                                                                                                                              
[72]   →fail unless 1 chkFeature'Solve Problem 3'                                                                                                                                                                                                                                                                                                                 
[73]   ⍝.                                                                                                                                                                                                                                                                                                                                                         
[74]  ⍝ Problem 4 – Just Meshing Around                                                                                                                                                                                                                                                                                                                           
[75]  
[76]  ⍝Write a dfn that takes vectors as its left and right arguments and returns them "meshed" into a single vector formed by alternately taking successive elements from each argument. The arguments do not have to be the same length.                                                                                                                        
[77]   ms4←{m←(⍴⍺)⌊⍴⍵ ⋄ (,↑[0.5]m∘↑¨⍺ ⍵),(m↓⍺),m↓⍵}                                                                                                                                                                                                                                                                                                               
[78]  ⍝Test cases:                                                                                                                                                                                                                                                                                                                                                
[79]                                                                                                                                                                                                                                                                                                                                                              
[80]  'MENS' {your_solution} 'EKES'                                                                                                                                                                                                                                                                                                                       
[81]  ⍝MEEKNESS                                                                                                                                                                                                                                                                                                                                                   
[82]   →fail unless aEq'MEEKNESS'on'MENS'ms4'EKES'                                                                                                                                                                                                                                                                                                                
[83]  'Dyalog' {your_solution} 'APL'                                                                                                                                                                                                                                                                                                                      
[84]  ⍝DAaPlLog                                                                                                                                                                                                                                                                                                                                                   
[85]   →fail unless aEq'DAyPaLlog'on'Dyalog'ms4'APL'                                                                                                                                                                                                                                                                                                              
[86]  'APL' {your_solution} 'Dyalog'                                                                                                                                                                                                                                                                                                                      
[87]  ⍝ADPyLalog                                                                                                                                                                                                                                                                                                                                                  
[88]   →fail unless aEq'ADPyLalog'on'APL'ms4'Dyalog'                                                                                                                                                                                                                                                                                                              
[89]  ⍝       1 3 5 7 {your_solution} 2 4 6 8 ⍝ should work with numeric vectors                                                                                                                                                                                                                                                                                  
[90]  ⍝1 2 3 4 5 6 7 8                                                                                                                                                                                                                                                                                                                                            
[91]   →fail unless aEq 1 2 3 4 5 6 7 8 on 1 3 5 7 ms4 2 4 6 8                                                                                                                                                                                                                                                                                                    
[92]  '' {your_solution} 'Hello' ⍝ either or both arguments could be empty                                                                                                                                                                                                                                                                                
[93]  ⍝Hello                                                                                                                                                                                                                                                                                                                                                      
[94]   →fail unless aEq'Hello'on''ms4'Hello'                                                                                                                                                                                                                                                                                                                      
[95]   →fail unless aEq'Hello'on'Hello'ms4''                                                                                                                                                                                                                                                                                                                      
[96]                                                                                                                                                                                                                                                                                                                                                              
[97]   →fail unless 1 chkFeature'Solve Problem 4'                                                                                                                                                                                                                                                                                                                 
[98]   ⍝.                                                                                                                                                                                                                                                                                                                                                         
[99]  ⍝ Problem 6 – Shorter Ones to the Front                                                                                                                                                                                                                                                                                                                     
[100] ⍝Write a dfn that takes a vector of vectors as its right argument and returns it sorted by the length of each element. Note: an element of the vector can be scalar or an empty vector.                                                                                                                                                                     
[101]  ms6←{⍵[⍋⍬∘⍴∘⍴∘,¨⍵]}                                                                                                                                                                                                                                                                                                                                        
[102] ⍝Test cases:                                                                                                                                                                                                                                                                                                                                                
[103] ⍝       {your_solution} 'one' 'two' 'three' 'four' 'five' 'six'                                                                                                                                                                                                                                                                                             
[104] ⍝┌───┬───┬───┬────┬────┬─────┐                                                                                                                                                                                                                                                                                                                              
[105] ⍝│one│two│six│four│five│three│                                                                                                                                                                                                                                                                                                                              
[106] ⍝└───┴───┴───┴────┴────┴─────┘                                                                                                                                                                                                                                                                                                                              
[107]  →fail unless aEq'one' 'two' 'six' 'four' 'five' 'three'on ms6'one' 'two' 'three' 'four' 'five' 'six'                                                                                                                                                                                                                                                       
[108] ⍝       {your_solution} (2 4 3) (4 5) 1 (7 3)                                                                                                                                                                                                                                                                                                               
[109] ⍝┌─┬───┬───┬─────┐                                                                                                                                                                                                                                                                                                                                          
[110] ⍝│1│4 5│7 3│2 4 3│                                                                                                                                                                                                                                                                                                                                          
[111] ⍝└─┴───┴───┴─────┘                                                                                                                                                                                                                                                                                                                                          
[112]  →fail unless aEq quote¨1(4 5)(7 3)(2 4 3)on ms6(2 4 3)(4 5)1(7 3)                                                                                                                                                                                                                                                                                          
[113] ⍝       {your_solution} ⍬ ⍝ should return an empty vector                                                                                                                                                                                                                                                                                                   
[114]  →fail unless aEq ⍬ on ms6 ⍬                                                                                                                                                                                                                                                                                                                                
[115] ⍝       {your_solution} 'one' 2 'three' '' 'four' (5 6 7 8)                                                                                                                                                                                                                                                                                                 
[116] ⍝┌┬─┬───┬────┬───────┬─────┐                                                                                                                                                                                                                                                                                                                                
[117] ⍝││2│one│four│5 6 7 8│three│                                                                                                                                                                                                                                                                                                                                
[118] ⍝└┴─┴───┴────┴───────┴─────┘                                                                                                                                                                                                                                                                                                                                
[119]  →fail unless aEq quote¨'' 2 'one' 'four'(5 6 7 8)'three'on ms6'one' 2 'three' '' 'four'(5 6 7 8)                                                                                                                                                                                                                                                           
[120]                                                                                                                                                                                                                                                                                                                                                             
[121]  →fail unless 1 chkFeature'Solve Problem 6'                                                                                                                                                                                                                                                                                                                 
[122]  ⍝.                                                                                                                                                                                                                                                                                                                                                         
[123] ⍝ Problem 7 – 3s and 5s                                                                                                                                                                                                                                                                                                                                     
[124] 
[125] ⍝Write a dfn that takes a numeric vector and returns all elements that are divisible by 3 or 5.                                                                                                                                                                                                                                                             
[126]  ms7←3 5∘{⍵/⍨0∨.=⍺∘.|⍵}                                                                                                                                                                                                                                                                                                                                     
[127] ⍝Test cases:                                                                                                                                                                                                                                                                                                                                                
[128] 
[129] ⍝       {your_solution} 1 2 3 4 5 6 7 8 9 10                                                                                                                                                                                                                                                                                                                
[130] ⍝3 5 6 9 10                                                                                                                                                                                                                                                                                                                                                 
[131]  →fail unless aEq 3 5 6 9 10 on ms7 1 2 3 4 5 6 7 8 9 10                                                                                                                                                                                                                                                                                                    
[132] ⍝       {your_solution} ⍬ ⍝ should return an empty vector                                                                                                                                                                                                                                                                                                   
[133]  →fail unless aEq ⍬ on ms7 ⍬                                                                                                                                                                                                                                                                                                                                
[134]                                                                                                                                                                                                                                                                                                                                                             
[135]  →fail unless 1 chkFeature'Solve Problem 7'                                                                                                                                                                                                                                                                                                                 
[136]  ⍝.                                                                                                                                                                                                                                                                                                                                                         
[137] ⍝Problem 8 – Separating Out the Negative                                                                                                                                                                                                                                                                                                                    
[138] 
[139] ⍝Write a dfn that takes a numeric vector and returns a two element vector whose first element contains the values less than 0 (zero) in the vector and the second element contains all values greater than or equal to 0.                                                                                                                                   
[140]  ms8←{1↓¨(0 1,⍵≥0){⊂⍵}quadEq 0 0,⍵}                                                                                                                                                                                                                                                                                                                         
[141] ⍝Test cases:                                                                                                                                                                                                                                                                                                                                                
[142] 
[143] ⍝       {your_solution} 0 1 ¯2 3 ¯4 ¯5 6 7 8 ¯9 10                                                                                                                                                                                                                                                                                                          
[144] ⍝┌───────────┬──────────────┐                                                                                                                                                                                                                                                                                                                               
[145] ⍝│¯2 ¯4 ¯5 ¯9│0 1 3 6 7 8 10│                                                                                                                                                                                                                                                                                                                               
[146] ⍝└───────────┴──────────────┘                                                                                                                                                                                                                                                                                                                               
[147]  →fail unless aEq(¯2 ¯4 ¯5 ¯9)(0 1 3 6 7 8 10)on ms8 0 1 ¯2 3 ¯4 ¯5 6 7 8 ¯9 10                                                                                                                                                                                                                                                                             
[148] ⍝       {your_solution} 1 2 3 4 5                                                                                                                                                                                                                                                                                                                           
[149] ⍝┌┬─────────┐                                                                                                                                                                                                                                                                                                                                               
[150] ⍝││1 2 3 4 5│                                                                                                                                                                                                                                                                                                                                               
[151] ⍝└┴─────────┘                                                                                                                                                                                                                                                                                                                                               
[152]  →fail unless aEq quote¨⍬(1 2 3 4 5)on ms8 1 2 3 4 5                                                                                                                                                                                                                                                                                                        
[153] ⍝       {your_solution} ⍬ ⍝ should return a vector of two empty vectors                                                                                                                                                                                                                                                                                     
[154] ⍝┌┬┐                                                                                                                                                                                                                                                                                                                                                        
[155] ⍝│││                                                                                                                                                                                                                                                                                                                                                        
[156] ⍝└┴┘                                                                                                                                                                                                                                                                                                                                                        
[157]  →fail unless aEq quote¨⍬ ⍬ on ms8 ⍬                                                                                                                                                                                                                                                                                                                        
[158]                                                                                                                                                                                                                                                                                                                                                             
[159]  →fail unless 1 chkFeature'Solve Problem 8'                                                                                                                                                                                                                                                                                                                 
[160] ⍝.                                                                                                                                                                                                                                                                                                                                                          
[161] 
[162] ⍝ Problem 9 – Delimited Text                                                                                                                                                                                                                                                                                                                                
[163] 
[164] ⍝It's common to encounter delimited text – for example, comma-separated values in a file.                                                                                                                                                                                                                                                                   
[165] 
[166] ⍝Write a dfn that takes a character vector as its right argument and one or more characters as its left argument, where those characters are delimiters in the right argument. The dfn should return the delimited text as a vector of vectors.                                                                                                             
[167]  ms9←{1↓¨(1,⍵∊⍺){⎕ML←1 ⋄ ⍺⊂⍵}' ',⍵}                                                                                                                                                                                                                                                                                                                         
[168] ⍝Test cases:                                                                                                                                                                                                                                                                                                                                                
[169] 
[170] ⍝       ',' {your_solution} 'comma,delimited,values'                                                                                                                                                                                                                                                                                                        
[171] ⍝┌─────┬─────────┬──────┐                                                                                                                                                                                                                                                                                                                                   
[172] ⍝│comma│delimited│values│                                                                                                                                                                                                                                                                                                                                   
[173] ⍝└─────┴─────────┴──────┘                                                                                                                                                                                                                                                                                                                                   
[174]  →fail unless aEq quote¨'comma' 'delimited' 'values'on','ms9'comma,delimited,values'                                                                                                                                                                                                                                                                        
[175] ⍝       ' ' {your_solution} 'break up words'                                                                                                                                                                                                                                                                                                                
[176] ⍝┌─────┬──┬─────┐                                                                                                                                                                                                                                                                                                                                           
[177] ⍝│break│up│words│                                                                                                                                                                                                                                                                                                                                           
[178] ⍝└─────┴──┴─────┘                                                                                                                                                                                                                                                                                                                                           
[179]  →fail unless aEq quote¨'break' 'up' 'words'on' 'ms9'break up words'                                                                                                                                                                                                                                                                                        
[180] ⍝       ',' {your_solution} ','                                                                                                                                                                                                                                                                                                                             
[181] ⍝┌┬┐                                                                                                                                                                                                                                                                                                                                                        
[182] ⍝│││                                                                                                                                                                                                                                                                                                                                                        
[183] ⍝└┴┘                                                                                                                                                                                                                                                                                                                                                        
[184]  →fail unless aEq quote¨'' ''on','ms9','                                                                                                                                                                                                                                                                                                                    
[185]                                                                                                                                                                                                                                                                                                                                                             
[186]  →fail unless 1 chkFeature'Solve Problem 9'                                                                                                                                                                                                                                                                                                                 
[187]  ⍝.                                                                                                                                                                                                                                                                                                                                                         
[188] ⍝ Problem 10 – Order Total / Dot Product                                                                                                                                                                                                                                                                                                                    
[189] 
[190] ⍝Suppose you have a numeric vector that is the list of prices for a set of retail products. You also have a numeric vector that is the number ordered of each product. Write a dfn that takes as its right argument a vector of prices and as its left argument a numeric vector that indicates the number ordered and returns the total cost for the order.
[191] 
[192] ⍝In case you hadn't realized it, this is an application of the dot product. The dot product of two vectors A = [A1, A2, ..., An] and B = [B1, B2, ..., Bn] is defined by the equation above.                                                                                                                                                                
[193]  msA←+.×                                                                                                                                                                                                                                                                                                                                                    
[194] ⍝Test cases:                                                                                                                                                                                                                                                                                                                                                
[195] 
[196] ⍝       5 0 2 {your_solution} 2.99 4.99 1.99                                                                                                                                                                                                                                                                                                                
[197] ⍝18.93                                                                                                                                                                                                                                                                                                                                                      
[198]  →fail unless aEq 18.93 on 5 0 2 msA 2.99 4.99 1.99                                                                                                                                                                                                                                                                                                         
[199] ⍝       0 0 0 {your_solution} 2.99 4.99 1.99                                                                                                                                                                                                                                                                                                                
[200] ⍝0                                                                                                                                                                                                                                                                                                                                                          
[201]  →fail unless aEq 0 on 0 0 0 msA 2.99 4.99 1.99                                                                                                                                                                                                                                                                                                             
[202]                                                                                                                                                                                                                                                                                                                                                             
[203]  →fail unless 1 chkFeature'Solve Problem 10'                                                                                                                                                                                                                                                                                                                
[204]  ⍝.                                                                                                                                                                                                                                                                                                                                                         
[205]  ⎕←ETC'      clipSet ⍵'∘{(quote DLTC ⍵)omrepl ⍺}ofE nlSORT ETC{⍵[where×{⍬⍴⍴(reMatch∘⍵)'m','s{{suff}}←'}ofE ⍵;]}⎕CR ThisFN''                                                                                                                                                                                                                                 
[206]  →fail unless 1 chkFeature'Show all solutions for clipping.'                                                                                                                                                                                                                                                                                                
[207]  ⍝⍝--                                                                                                                                                                                                                                                                                                                                                       
[208]  :If ~1 chkFeature tfuuN omrepl'implement "⍵"'                                                                                                                                                                                                                                                                                                              
[209]      ⎕←APLPmtMFS fThis'TEST_',tfuuN                                                                                                                                                                                                                                                                                                                         
[210]      →XIT                                                                                                                                                                                                                                                                                                                                                   
[211]  :EndIf                                                                                                                                                                                                                                                                                                                                                     
[212]  r←1                                                                                                                                                                                                                                                                                                                                                        
[213]  →XIT                                                                                                                                                                                                                                                                                                                                                       
[214] fail2:                                                                                                                                                                                                                                                                                                                                                      
[215]  ⎕←APLPmtMFS tfuuN                                                                                                                                                                                                                                                                                                                                          
[216]  ⎕←0 APLPmt ThisFN''                                                                                                                                                                                                                                                                                                                                        
[217]  →XIT                                                                                                                                                                                                                                                                                                                                                       
[218] fail:                                                                                                                                                                                                                                                                                                                                                       
[219]  ⎕←APLPmtMFS fThis ThisFN''                                                                                                                                                                                                                                                                                                                                 
[220] XIT: ⍝⍝-------                                                                                                                                                                                                                                                                                                                                              
[221]  test_complete                                                                                                                                                                                                                                                                                                                                              
     ∇                                                                                                                                                                                                                                                                                                                                                            

Page served at: Wed, 08 Apr 2020 05:09:40 AST.