From 34c65951fbe26d7ab9165a97fe50a89a9576e908 Mon Sep 17 00:00:00 2001 From: Udo Borkowski Date: Fri, 25 May 2018 13:21:24 +0200 Subject: [PATCH 1/3] add "HeatWaves-readable" for the location of the "readable" version To help a reader understand the "short" code version a developer may add the "long" version, with comments, longer variable names etc. to "isHeatWaveIncludedIn_readable" in "HeatWaves-readable.swift" --- CocoaheadsCodeGolf.xcodeproj/project.pbxproj | 4 +++ HeatWaves/HeatWaves-readable.swift | 34 ++++++++++++++++++++ HeatWaves/HeatWaves.swift | 2 ++ 3 files changed, 40 insertions(+) create mode 100644 HeatWaves/HeatWaves-readable.swift diff --git a/CocoaheadsCodeGolf.xcodeproj/project.pbxproj b/CocoaheadsCodeGolf.xcodeproj/project.pbxproj index 30e7a3b..5ef1d71 100644 --- a/CocoaheadsCodeGolf.xcodeproj/project.pbxproj +++ b/CocoaheadsCodeGolf.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 269A58DE20B1822300872989 /* HeatWavesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269A58DD20B1822300872989 /* HeatWavesTests.swift */; }; 269A58E020B1822300872989 /* HeatWaves.h in Headers */ = {isa = PBXBuildFile; fileRef = 269A58D220B1822300872989 /* HeatWaves.h */; settings = {ATTRIBUTES = (Public, ); }; }; 269A58E820B1823C00872989 /* HeatWaves.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269A58E720B1823C00872989 /* HeatWaves.swift */; }; + 3448F28C20B80F38008CC209 /* HeatWaves-readable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3448F28B20B80F38008CC209 /* HeatWaves-readable.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -32,6 +33,7 @@ 269A58DF20B1822300872989 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 269A58E720B1823C00872989 /* HeatWaves.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeatWaves.swift; sourceTree = ""; }; 269A58ED20B2D13D00872989 /* CharacterCount.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CharacterCount.txt; sourceTree = ""; }; + 3448F28B20B80F38008CC209 /* HeatWaves-readable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HeatWaves-readable.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -78,6 +80,7 @@ 269A58E720B1823C00872989 /* HeatWaves.swift */, 269A58ED20B2D13D00872989 /* CharacterCount.txt */, 269A58D320B1822300872989 /* Info.plist */, + 3448F28B20B80F38008CC209 /* HeatWaves-readable.swift */, ); path = HeatWaves; sourceTree = ""; @@ -218,6 +221,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 3448F28C20B80F38008CC209 /* HeatWaves-readable.swift in Sources */, 269A58E820B1823C00872989 /* HeatWaves.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/HeatWaves/HeatWaves-readable.swift b/HeatWaves/HeatWaves-readable.swift new file mode 100644 index 0000000..554083e --- /dev/null +++ b/HeatWaves/HeatWaves-readable.swift @@ -0,0 +1,34 @@ +// +// HeatWaves-readable.swift +// HeatWaves +// +// Created by Sven Titgemeyer on 20.05.18. +// Edited by Udo Borkowski on 2018-05-25. +// + +// Background +// +// The Royal Netherlands Meteorological Institute defines a heat wave* as a +// series of at least 5 consecutive days of ≥25°C weather (“summery weather”), +// such that at least 3 of those days are ≥30°C (“tropical weather”). +// +// The tropical weather doesn't have to be measured consecutively: for example: +// 30, 25, 30, 26, 27, 28, 32, 30 is a 8-day long heat wave with 4 days of +// tropical weather. +// +// *(Well, by Dutch standards.) +// +// Challenge +// +// Given a non-empty list of positive integers representing Celsius temperature +// measurements from successive days, decide whether that list contains a heat +// wave (as per the above definition). +// +// The shortest answer in bytes wins. +// +// (This function contains the same code/algorithm as 'isHeatWaveIncludedIn' +// in 'HeadWaves.swift' but is intended for a reader who wants to understand +// the code better. So it includes comments, whitespaces, better names, ....) +public func isHeatWaveIncludedIn_readable(waves w: [Int]) -> Bool { + return false // no head wave found +} diff --git a/HeatWaves/HeatWaves.swift b/HeatWaves/HeatWaves.swift index b32c34e..f491f17 100644 --- a/HeatWaves/HeatWaves.swift +++ b/HeatWaves/HeatWaves.swift @@ -25,6 +25,8 @@ // wave (as per the above definition). // // The shortest answer in bytes wins. +// +// (see "HeadWaves-readable.swift" for a readable/commented version of this code) public func isHeatWaveIncludedIn(waves w: [Int]) -> Bool { return false } From 2c962b627fbc51dc64a6051443520fc8324df9de Mon Sep 17 00:00:00 2001 From: Udo Borkowski Date: Fri, 25 May 2018 18:21:16 +0200 Subject: [PATCH 2/3] fix typos --- HeatWaves/HeatWaves-readable.swift | 4 ++-- HeatWaves/HeatWaves.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HeatWaves/HeatWaves-readable.swift b/HeatWaves/HeatWaves-readable.swift index 554083e..1a67559 100644 --- a/HeatWaves/HeatWaves-readable.swift +++ b/HeatWaves/HeatWaves-readable.swift @@ -27,8 +27,8 @@ // The shortest answer in bytes wins. // // (This function contains the same code/algorithm as 'isHeatWaveIncludedIn' -// in 'HeadWaves.swift' but is intended for a reader who wants to understand +// in 'HeatWaves.swift' but is intended for a reader who wants to understand // the code better. So it includes comments, whitespaces, better names, ....) public func isHeatWaveIncludedIn_readable(waves w: [Int]) -> Bool { - return false // no head wave found + return false // no heat wave found } diff --git a/HeatWaves/HeatWaves.swift b/HeatWaves/HeatWaves.swift index f491f17..f5bea7d 100644 --- a/HeatWaves/HeatWaves.swift +++ b/HeatWaves/HeatWaves.swift @@ -26,7 +26,7 @@ // // The shortest answer in bytes wins. // -// (see "HeadWaves-readable.swift" for a readable/commented version of this code) +// (see "HeatWaves-readable.swift" for a readable/commented version of this code) public func isHeatWaveIncludedIn(waves w: [Int]) -> Bool { return false } From 0965fb07f00f7587fa8e9b536298534a1e8ba1ea Mon Sep 17 00:00:00 2001 From: Udo Borkowski Date: Fri, 25 May 2018 20:34:39 +0200 Subject: [PATCH 3/3] An improved "Reduce-Factors" solution "too complex" for Swift 4.1 (80 bytes) Swift 4.1 will not compile this code but report the error: "Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions" Maybe a later version of the Swift compiler can handle this... --- HeatWaves/CharacterCount.txt | 2 +- HeatWaves/HeatWaves-readable.swift | 32 +++++++++++++++++++++++++++--- HeatWaves/HeatWaves.swift | 14 ++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/HeatWaves/CharacterCount.txt b/HeatWaves/CharacterCount.txt index f837576..cc9db5f 100644 --- a/HeatWaves/CharacterCount.txt +++ b/HeatWaves/CharacterCount.txt @@ -1 +1 @@ -12 bytes. +80 bytes. diff --git a/HeatWaves/HeatWaves-readable.swift b/HeatWaves/HeatWaves-readable.swift index 1a67559..00fab46 100644 --- a/HeatWaves/HeatWaves-readable.swift +++ b/HeatWaves/HeatWaves-readable.swift @@ -2,8 +2,9 @@ // HeatWaves-readable.swift // HeatWaves // -// Created by Sven Titgemeyer on 20.05.18. -// Edited by Udo Borkowski on 2018-05-25. +// Solution by Udo Borkowski (2018-05-25) +// +// FOR THE PUBLIC DOMAIN // // Background @@ -30,5 +31,30 @@ // in 'HeatWaves.swift' but is intended for a reader who wants to understand // the code better. So it includes comments, whitespaces, better names, ....) public func isHeatWaveIncludedIn_readable(waves w: [Int]) -> Bool { - return false // no heat wave found + + // This solution is similar to "solution-087-Reduce-Factors" but makes use of an + // extra nested closure. It looks like this is "too much" for the current + // Swift compiler (4.1) as it reports the error: + // + // Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions + // + // Too bad, as this is an 80 bytes solution ! + // + // BTW: as suggested in the error message breaking up the expression solves the issue. + // E.g. if we extract the nested closure into an outer variable 'f': + // + // let f={$0%27==0&&$0>81 ?0:$0};return w.reduce(1,{f($1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0)})<1 + // + // the code compiles and the tests are green. However we are now at 88 bytes! + // + + // Expect a compile error in the next line. See comment above... + return w.reduce(1,{($1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0){$0%27==0&&$0>81 ?0:$0}})<1 + + // This solution is not yet fully documented. but you may have a look at "solution-087-Reduce-Factors" + // to get an idea how it works. The following lines align both solutions and show the differences: + // + // "solution-080-TooComplex" return w.reduce(1,{( $1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0){ $0%27==0&&$0>81 ?0:$0}})<1 + // "solution-087-Reduce-Factors" return w.reduce(1,{let n=$1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0;return n %27==0&&n >81 ?0:n })<1 + } diff --git a/HeatWaves/HeatWaves.swift b/HeatWaves/HeatWaves.swift index f5bea7d..fc48aa8 100644 --- a/HeatWaves/HeatWaves.swift +++ b/HeatWaves/HeatWaves.swift @@ -2,8 +2,9 @@ // HeatWaves.swift // HeatWaves // -// Created by Sven Titgemeyer on 20.05.18. -// Copyright © 2018 Cocoaheads Aachen. All rights reserved. +// Solution by Udo Borkowski (2018-05-25) +// +// FOR THE PUBLIC DOMAIN // // Background @@ -27,6 +28,13 @@ // The shortest answer in bytes wins. // // (see "HeatWaves-readable.swift" for a readable/commented version of this code) +// +// NOTICE: The following code gives a compile error when running in Xcode 4.1 (Swift 4.1): +// +// "Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions" +// +// This is most likely caused by Swift's algorithm that resolves overloaded functions. In case this algorithm +// is improved in a later Swift version this code may compile and run.... public func isHeatWaveIncludedIn(waves w: [Int]) -> Bool { - return false + return w.reduce(1,{($1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0){$0%27==0&&$0>81 ?0:$0}})<1 }