Why I love Python
Recently had to do an excercise and picked up Java, first solution was very readable but also verbose and probably not optimized, so I decided to review it in a more concise way, no doubt there are more concise ways in Java. Of course curiosity on how that would look like in Python caught me and so here I share the three code samples, omitting all comments to focus on the code and it's not important to know what the code does, but trust me, they all do the same thing. I love Python because I had to think less.
Solution #1 Java : First breakdown of the solution
public static String solution(int[] T) {
int days=T.length;
int daysInSeason=days/4;
int winterStart=0;
int winterFinish = winterStart + daysInSeason - 1;
int springStart = winterFinish + 1;
int springFinish = springStart + daysInSeason - 1;
int summerStart = springFinish + 1;
int summerFinish = summerStart + daysInSeason - 1;
int autumnStart = summerFinish + 1;
int autumnFinish = autumnStart + daysInSeason - 1;
List<Integer> seasonIndexes = new ArrayList<>();
for (int idxs = 0; idxs < days; idxs = idxs + daysInSeason) {
seasonIndexes.add(idxs);
seasonIndexes.add(idxs+daysInSeason-1);
}
int[] winterSeason=Arrays.copyOfRange(T,winterStart,winterFinish+1);
int[] springSeason=Arrays.copyOfRange(T,springStart,springFinish+1);
int[] summerSeason=Arrays.copyOfRange(T,summerStart,summerFinish+1);
int[] autumnSeason=Arrays.copyOfRange(T,autumnStart,autumnFinish+1);
int winterMin=Arrays.stream(winterSeason).min().getAsInt();
int springMin=Arrays.stream(springSeason).min().getAsInt();
int summerMin=Arrays.stream(summerSeason).min().getAsInt();
int autumnMin=Arrays.stream(autumnSeason).min().getAsInt();
int winterMax=Arrays.stream(winterSeason).max().getAsInt();
int springMax=Arrays.stream(springSeason).max().getAsInt();
int summerMax=Arrays.stream(summerSeason).max().getAsInt();
int autumnMax=Arrays.stream(autumnSeason).max().getAsInt();
int winterRange=winterMax-winterMin;
int springRange=springMax-springMin;
int summerRange=summerMax-summerMin;
int autumnRange=autumnMax-autumnMin;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("WINTER", winterRange);
map.put("SPRING", springRange);
map.put("SUMMER", summerRange);
map.put("AUTUMN", autumnRange);
return(map.entrySet().stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().getKey());
}
Solution #2 Java : More concise code
public static String evenBetterSolution(int[] T) {
int days=T.length;
int daysInSeason=days/4;
List<HashMap<String, Integer>> listOfSeasons = new ArrayList<HashMap<String, Integer>>();
HashMap<String,Integer> tempItem;
vector of seasons
for (int idx = 0; idx < 4; idx++)
{
tempItem=new HashMap<String,Integer>();
tempItem.put("iStart",(idx)*(daysInSeason));
tempItem.put("iEnd",(idx+1)*(daysInSeason)-1);
tempItem.put("minTemp",Arrays.stream(Arrays.copyOfRange(T,tempItem.get("iStart"),tempItem.get("iEnd")+1)).min().getAsInt());
tempItem.put("maxTemp",Arrays.stream(Arrays.copyOfRange(T,tempItem.get("iStart"),tempItem.get("iEnd")+1)).max().getAsInt());
tempItem.put("range", tempItem.get("maxTemp")-tempItem.get("minTemp"));
listOfSeasons.add(tempItem);
System.out.println(listOfSeasons.get(idx).toString());
}
Map<String, Integer> seasonRanges = new HashMap<String, Integer>();
create a mapping before indexes and season at some point
seasonRanges.put("WINTER", listOfSeasons.get(0).get("range"));
seasonRanges.put("SPRING", listOfSeasons.get(1).get("range"));
seasonRanges.put("SUMMER", listOfSeasons.get(2).get("range"));
seasonRanges.put("AUTUMN", listOfSeasons.get(3).get("range"));
return(seasonRanges.entrySet().stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().getKey());
}
Solution #3 Python : Then you have the Python way
def solution(vTemp):
daysInSeason=int(len(vTemp)/4)
seasonsT={0:{"seasonName":"WINTER","seasonRange":0},1:{"seasonName":"SPRING","seasonRange":0},2:{"seasonName":"SUMMER","seasonRange":0},3:{"seasonName":"AUTUMN","seasonRange":0}}
for idx in range(4):
seasonsT[idx][list(seasonsT[idx])[1]]=(max(vTemp[idx*daysInSeason:(idx+1)*daysInSeason])-min(vTemp[idx*daysInSeason:(idx+1)*daysInSeason]))
print(seasonsT[max(seasonsT, key=lambda v: seasonsT[v]['seasonRange'])]["seasonName"])
solution(vTemperatures)
"they all do the same thing" - they crash with runtime error if the input array is too small (less than 4 elements), and print "WINTER" otherwise? I can write a C program which is more compact than Python, and does the same thing :) I guess the description of the problem is missing here, so one could try what you did, too