Wednesday, June 22, 2022

Day 9/100 Higher and Lower

 Day 9/100 Higher and Lower

Levels and Sequences


Last time, on Day 8, I explored LeetCode 845 and 2210. I'm continuing the exploration with LeetCode 300 and 315. I'm going to do 315 first, since that one is easy. All I have to do is basically take the value of each data in an array, and check whether the subsequent value is higher or lower from that initial value.


LeetCode 315 specifies only to count the number of lower values, but since it's so easy, I do it both for higher and lower values. Taking the diff from pnf.c (Day 7) and LeetCode 315:


LeetCode 315


29a30,35
> #define MAXENTRY 10000
> float DataV[MAXENTRY];
> int DataL[MAXENTRY];
> int DataH[MAXENTRY];
> //tstamp is num of entry

100a107,123
> void Count() {
>   int i=0;
>   for (i=0;i<=tstamp;i++) {
>     if (i==tstamp) { DataV[i]=Adj; DataL[i]=DataH[i]=0; }
>     if (i<tstamp && DataV[i]>Adj) DataL[i]++;
>     if (i<tstamp && DataV[i]<Adj) DataH[i]++;
>   }
> }

> void CountDisp() {
>   int i=0;
>   for (i=0;i<tstamp;i++) {
>     printf("Entry %3d value %f\t LowCount: %3d\tHiCount:%3d\n"
>            ,i,DataV[i],DataL[i],DataH[i]);
>   }
> }

106a130,132

>     Count();

135a162,163

>   CountDisp();


That's too easy! I count only 8 line significant difference from the original. Really, there's nothing to explain since it's self explanatory. Anyway, onto LeetCode 300. This time, take LeetCode 315 and patch this diff in:


LeetCode 300


111,112c111,112
<     if (i<tstamp && DataV[i]>Adj) DataL[i]++;
<     if (i<tstamp && DataV[i]<Adj) DataH[i]++;
---
>     if (i<tstamp && DataV[i]>Adj) { DataL[i]++; DataV[i]=Adj; }
> //    if (i<tstamp && DataV[i]<Adj) { DataH[i]++; DataV[i]=Adj; }
117c117
<   int i=0;
---
>   int i=0; int max=0;
119,120c119,121
<     printf("Entry %3d value %f\t LowCount: %3d\tHiCount:%3d\n"
<            ,i,DataV[i],DataL[i],DataH[i]);
---
>     printf("Entry %3d value %f\t LowCount: %3d \tHiCount: %3d\n",i,DataV[i],DataL[i],DataH[i]);
>     if (max<DataL[i]) max++;
> //  if (max<DataH[i]) max++;
121a123
>   printf("Longest sequence is: %d\n",max);



As far as the longest sequence is, I'm only counting one or the other. I could have counted both at once, but that requires an extra storage for DataV[]. The reason being is that although the algorithm is the same, for tracking sequence, the value of DataV[] is updated every time there is a count. And that's about the only significant difference between the two.


So, you choose: You can either count the low, the high, or put in an extra array variable to count both.


As you can see, leveraging existing program can do wonders for your development time. This is the concept behind "code reuse" although I suppose the term is more closely associated with either code library or objects (in OO paradigm), as opposed to hacking old program to do something new.


Experience speaking, sometimes the necessary change is rather drastic, and you need to change the program's core. In that case, it may be easier, faster, and better to just start a new program from scratch.


Happy coding!


No comments:

Post a Comment