Some companies like to pose certain programming questions as part of their interview process.
I’ve run into a variety of these. They’ve been SQL related questions, code writing, and algorithm descriptions. Some were fairly straight forward. Some less so.
One of the best I heard was a series of “How would you write this code if you had to write it …” with varying restrictions put on things. And the code was pretty simple. Write code to count all the upper and lower case letters in a string of ASCII characters.
First write the code however you wanted. Loops, if then else, select case.
Depending on which branching style you used the first time then the follow on was to write it using the other. If you first used a massive if then elseif statement then switch to using a select case.
And of course take into account that Xojo IS not case sensitive with string comparisons.
And finally to not use a branching type at all. This requires some out of the box thinking. Literally “count all the upper and lower case ASCII letters without the use of IF, SELECT or any other branching control.” All you have to use is a loop (while/wend, for/next whatever you wanted)
This first two bits are fairly straight forward.
The third however is still one of my favourite “thinking out of the box” approaches.
Anyone care to take a guess at how to achieve that one ?
I am going to make some assumptions since you didn’t 100% define the environment
one being that a binary compare returns 0 or 1
dim bucket(1) as Integer
S=”AStringWithUpperAndLowerCaseCharacters”
for i=1 to S.Len
bucket(ABS((ASC(mid(s,i,1)>ASC(“Z”)))+=1
next i
pseudo code just to illustrate my idea
Split string
Save ASC of each char in database
Count all ASC in lowercase or uppercase range
How am I doing? 😉
guess my comments never got posted here… so I will try once more
I am going to make some assumptions since you didn’t 100% define the environment
one being that a binary compare returns 0 or 1
dim bucket(1) as Integer
S=”AStringWithUpperAndLowerCaseCharacters”
for i=1 to S.Len
bucket(ABS((ASC(mid(s,i,1)>ASC(“Z”)))+=1
next i
pseudo code just to illustrate my idea
If there we’re ONLy ascii chars (no numbers or symbols)
Dim LowerChars, UpperChars, theByte as integer
Dim CharArr() as String = Split(“123abc123ABCD”,””)
For each theChar as String in CharArr
theByte =Asc(theChar)
LowerChars = LowerChars + (thebyte\97) – (thebyte\123)
UpperChars = UpperChars + (thebyte\65) – (theByte\91)
Next
Forgot to edit the text… It work if only ascii shares even if #s or symbols are present
Norman?? are you deleting my solution? I have posted it twice, and twice it has vanished…
BTW did you have different solution than the above?
-karen
Ok, the suspense is killing me … 🤯 … Norman, what did you come up with???
This wasnt my invention. It was one related to me. And there are several answers that will work. Those presented work just fine in Xojo
I’ve seen answers like these as well as others including an array of delegates. The array has 256 entries (from 0 to 255) and the index is the ASCB of the byte. The delegates are references to 2 methods (CountUpper and CountLower) that do just that