❔ I need help with Binary search alphabetically

Guys, I have this assignment where they want me to implement this Binary search algo
Console.WriteLine("\n\tWhich letter should the search start with?");
string key = Console.ReadLine(); ; // Receives the user's search
if (key.Length <= 0) key = "a"; // Makes sure the search is always something, in this case "a"
/*
* BINARY SEARCH using letters
*
* Based on the new tools we've gone through before (CompareTo and extracting single chars from strings for comparisons) so
* here we should write a binary search to look for letters.
*
*/
LetterPrint(wordList); // Print the entire list using our method.
Console.WriteLine("\n\tWhich letter should the search start with?");
string key = Console.ReadLine(); ; // Receives the user's search
if (key.Length <= 0) key = "a"; // Makes sure the search is always something, in this case "a"
/*
* BINARY SEARCH using letters
*
* Based on the new tools we've gone through before (CompareTo and extracting single chars from strings for comparisons) so
* here we should write a binary search to look for letters.
*
*/
LetterPrint(wordList); // Print the entire list using our method.
114 Replies
phaseshift
phaseshift2y ago
Ok. And?
The king of kings
I recently, learned Binary search for list of integers & strings, but now I'm trying to search alphabetically. My question remains, how do I start or should I covert type int to char or something?
Jimmacle
Jimmacle2y ago
why did you start a new thread and what looks like throw away what you already had? https://discord.com/channels/143867839282020352/1099919832419422218 if you already got it working for 2 data types then it should be easy to do a third
Henkypenky
Henkypenky2y ago
here we go again.. array of strings? then look for first letter of the word that matches the character provided see String.StartsWith(Char) modify your binary search to include this
The king of kings
My friend, this is another project, it's not belong to the one that I'm building currently. That one is for searching strings, but this one should be searching alphabetically.
Henkypenky
Henkypenky2y ago
dude, binary search is binary search there is no other binary search learn the concept not the task better yet, try to understand it binary search is: from a sorted list of items, repeatedly halve it until you find what you are looking for that's it
Jimmacle
Jimmacle2y ago
i also don't know what the difference is supposed to be between "searching strings" and "searching alphabetically" you generally compare strings according to some kind of alphabetical order
The king of kings
I do understand the concept and you're right. It's the same pattern for all implementation, I agree with you.
Henkypenky
Henkypenky2y ago
piece of advice you need to start thinking more i've been following your posts
The king of kings
Well! You need to ask the course host not me 😆 they're the one who send me the assignment.
Henkypenky
Henkypenky2y ago
and everyone has been helping you it's time you rise up and learn for yourself and the next thing you ask here is in the form of: I did this, and that and that, and also this, and it's not working because of this error, but i think i can solve it by doing this, what do you think?,,,,,,,,,,,,,,,,, instead of here is my assignment, what do i do? THINK!!!!!!!!!!!!!!! THINK THINK THINK START THINKING FOR YOURSELF
The king of kings
Ok! I guess when I search on Google and don't find the answer or the issue that I'm solving and when I can't find the answer, this is the last place I would need to reach for the solving the issue.
Henkypenky
Henkypenky2y ago
that's an excuse i'm not saying don't ask for help
Jimmacle
Jimmacle2y ago
there are tons of times where google won't give you a copy and paste solution
The king of kings
Yeah! That's right.
Jimmacle
Jimmacle2y ago
but you can still take what you find and try to apply it to your problem
Henkypenky
Henkypenky2y ago
you know how i learn? by failing over and over and over again there is no shame in that
JakenVeina
JakenVeina2y ago
evening
Jimmacle
Jimmacle2y ago
i'm currently teaching myself labview for work by going through existing applications with basically no documentation i don't have google to help me
JakenVeina
JakenVeina2y ago
awesome
Jimmacle
Jimmacle2y ago
i just poke at stuff until it breaks, then figure out why it did that then i learn
JakenVeina
JakenVeina2y ago
I've been there
Henkypenky
Henkypenky2y ago
exactly my breakfast love it
Jimmacle
Jimmacle2y ago
which is how i learned C# too fwiw making shitty winforms calculators just to try stuff out
The king of kings
Really! That's very impressive lol.
Jimmacle
Jimmacle2y ago
IMO it's not impressive, it's a standard skill that any programmer needs to have
The king of kings
Yeah! You need to account on yourself and try things out on your own without going to the Google or tutorials directly. I think Discord is wasting a lot of my time, I would rather go try things on my own or Google things.
Jimmacle
Jimmacle2y ago
from what i've seen you definitely need to spend more time trying to develop a solution yourself before asking for a solution here see: this thread where you didn't even seem to attempt to solve it
JakenVeina
JakenVeina2y ago
well I'm not gonna say you shouldn't come here for help like don't hesitate to come for help but fixed is kinda right you don't show much effort in trying to think through this problem for yourself and if that's the core problem, then that's what you need to be asking here
Jimmacle
Jimmacle2y ago
it's also a lot easier for people to help you if you can show them something you've tried and then they can give you direction from there
JakenVeina
JakenVeina2y ago
"I need help learning how to logically think through this problem" cause, yeah, after the past week, that's what you need you need to learn to think like a computer it's absolutely a skill, and there's no shame in not being good at it unless you're not willing to work on it
The king of kings
You guys are giving me a really good advices, I will start a new strategy from now own and develop my solving problem & logical thinking skills. Getting tips from skilled people like you is very valuable and can make a big progress in my learning how to code journey. Thank you
JakenVeina
JakenVeina2y ago
do you want help figuring out this BinarySearch method?
The king of kings
I guess this is what developers do and this is what they get paid for, building softwares by solving problems and write logic. They don't go to Discord so often to ask people or Google all the time, they use their experience & their skills.
JakenVeina
JakenVeina2y ago
yes and no if you think I don't goolge shit regularly, you're wrong shit I've done before shit I do regularly "Google" is just one of the ubiquitous tools, and yeah, there were programmers before Google but they didn't do shit blind or from memory technical jobs thrive on technical documentation before Google, you'd be regularly referencing your bookshelf of textbooks on various programming languages, machine interfaces, etc.
The king of kings
I have learned the Binary search and how it works, but I'm wondering about like comparing alphabetically.
JakenVeina
JakenVeina2y ago
Google is generally not my "first" stop for something, it's more likely docs.microsoft or MDN or whatever other site has technical documentation for the thing I'm trying to use comparing what alphabetically? what do you think that means? how is the binary search algorithm that you've now "learned" not doing that?
The king of kings
Ok! Very interesting informations. You're right, I wonder how people were programming before without Google or Youtube, some guy told me here, I learned coding where the Discord didn't exist. comparing alphabetically means following the words in alphabetical order A B C D Adam Bob Conor David
JakenVeina
JakenVeina2y ago
okay again you've now written a binary search algorithm, yes? how is it not doing that? cause it is
The king of kings
Ok! Good point.
JakenVeina
JakenVeina2y ago
that's what StringComparer does something we've gone over several times now it compares strings by character and you then use those comparisons to perform the search
The king of kings
So this is what the method is doing
StringComparer.OrdinalIgnoreCase.Compare(blogTitles[middle][0], element);
StringComparer.OrdinalIgnoreCase.Compare(blogTitles[middle][0], element);
JakenVeina
JakenVeina2y ago
yes
The king of kings
Ok
JakenVeina
JakenVeina2y ago
you are using the instance of StringComparer that performs comparisons by ordinal character comparison, I.E. raw byte value comparison except it also ignores case and as discussed yesterday, don't, use StringComparer.CurrentCulture or StringComparer.Ordinal
The king of kings
Also CompareTo() too as they mentioned
JakenVeina
JakenVeina2y ago
and since the byte values of Unicode characters are ordered alphabetically, that gets you alphabetical comparison ah okay so, they want you to use a .CompareTo() method, specifically if that's part of the assignment, then that's part of the assignment
The king of kings
Aha! You're right. Otherwise in what order it should compare, like there's nothing else to compare other than alphabetical order.
JakenVeina
JakenVeina2y ago
String.CompareTo Method (System)
Compares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String.
JakenVeina
JakenVeina2y ago
long-story-short, the string.CompareTo() method functions exactly as StringComparer.Compare() it's just a method upon string, instead of on a separate StringComparer instance so if you were using
StringComparer.OrdinalIgnoreCase.Compare(blogTitles[middle][0], element);
StringComparer.OrdinalIgnoreCase.Compare(blogTitles[middle][0], element);
that is an easy translation to use .CompareTo() instead
The king of kings
Man, I'm just jealous of you and the level of knowledge that you're capable of in coding. You're these alphabetical and the Binary machine code zeros, ones 00000110 and the chars and so on.. are mentioned in the ASCI too
JakenVeina
JakenVeina2y ago
practice
The king of kings
Ok! Great explanation, good job man. Then I'll use CompareTo() in my logic.
JakenVeina
JakenVeina2y ago
let's see it
The king of kings
I'll implement the project and show you what I have done
JakenVeina
JakenVeina2y ago
nah, just start with that one line
StringComparer.OrdinalIgnoreCase.Compare(blogTitles[middle][0], element);
StringComparer.OrdinalIgnoreCase.Compare(blogTitles[middle][0], element);
translate this line to use .CompareTo() instead ask questions if you need guidance
The king of kings
Ok! Let me show you. You're awesome bro, thanks a lot. Does the ordinal needs to be included or not in this context? String.CompareTo(list<strings> myList, target)
JakenVeina
JakenVeina2y ago
nah, there's no real equivalent per documentation
Both overloads of the CompareTo method perform culture-sensitive and case-sensitive comparison. You cannot use this method to perform culture-insensitive or ordinal comparisons.
The king of kings
what is culture sensitive means?
JakenVeina
JakenVeina2y ago
not ordinal I mean, I don't FULLY know
The king of kings
Aha ok
JakenVeina
JakenVeina2y ago
my guess would be like
The king of kings
Ok
JakenVeina
JakenVeina2y ago
if you compare, like, an "a" against an "a" with an accent ordinally, those are very far apart like, the "a" with an accent would ordinally come after "b"
The king of kings
does it mean like every culture write things differently or something
JakenVeina
JakenVeina2y ago
it means there are lots of different cultural rules, yes "current" culture means .NET is going to look at the settings on your PC to figure out what to use
The king of kings
Ok I got an idea about this topic I don't think it plays a very big roll
JakenVeina
JakenVeina2y ago
culture?
The king of kings
case sensitive I would say is more relevant particularly in Binary searching yes
JakenVeina
JakenVeina2y ago
right
The king of kings
What do you think my friend?
JakenVeina
JakenVeina2y ago
well, you're not using .CompareTo() on line..... 11?
The king of kings
You mean I should've included the middle variable inside the brackets? on line 11 I'm basically comparing if the element is equal to the middle of our list
JakenVeina
JakenVeina2y ago
right
on line 11 I'm basically comparing if the element is equal to the middle of our list
you didn't use .CompareTo()
Pobiega
Pobiega2y ago
string.CompareTo(Object) is going to give you pain and thats what you are doing atm
JakenVeina
JakenVeina2y ago
that too that's just flat-out wrong
Pobiega
Pobiega2y ago
you also have the wrong return type, again. and you need to use conditionals (if or switch) and adjust your left/right based on the returnvalue from your compare
JakenVeina
JakenVeina2y ago
I mean, it's an in-progress implementation can we focus on one thing at a time?
Pobiega
Pobiega2y ago
Its just that this binary search mess has been going on for over a week at this point.
JakenVeina
JakenVeina2y ago
indeed there's a certain amount of time-zone mismatch, I think
Pobiega
Pobiega2y ago
Not for me, me and Faraj are both based in Sweden.
The king of kings
If it was that easy it wouldn't take a week. It took a week, but the knowledge is permanent.
The king of kings
This seems a good example of what I'm doing currently
Pobiega
Pobiega2y ago
sure.
The king of kings
Great
Anton
Anton2y ago
You're comparing them twice. Remove the equals check == and just use CompareTo Since you have to use an ignore case comparison, forget == exists for strings
The king of kings
What 🙆🏻‍♂️ I didn't notice that. You're right. So this is why it says you can't convert string to int because middle variable is int. I have a bug and it's really pain in ass
The king of kings
The random method generates, and the algorithm sorts, but then it says in Swedish the list wasn't sorted, you need to sort first.
The king of kings
It doesn't even checks the dame first if statement
Jimmacle
Jimmacle2y ago
so what's the first step in finding out why it's printing that message?
The king of kings
good question
Jimmacle
Jimmacle2y ago
i'm asking you
The king of kings
I know lol Well! There are two assumptions here. I don't really know actually I thought about some assumptions that could be the issues, but I don't think they're the problem. Very weird I'm sure the bug is located in the case 6 in the if conditions
Jimmacle
Jimmacle2y ago
the first step is to use your debugger and set a breakpoint on the line that you think is writing the message
The king of kings
Since the default condition statement else is working, that means the problem is in the if statement
Jimmacle
Jimmacle2y ago
and verify that your assumption is correct because just a minute ago you sent two screenshots of two different places in the code that can print that message
The king of kings
This is the problem man, I don't come here and ask for help directly. But this is my issue and why I keep asking many questions in Discord is that I'm using VS on Mac and the debugger is not very effective as on VS on windows. Look Yes, but they're the same. Let me debug and show you
Jimmacle
Jimmacle2y ago
what should i be seeing here? it looks like the breakpoint worked like it should
Jimmacle
Jimmacle2y ago
still not seeing what's wrong with your debugger, that looks fine
The king of kings
so is that mean it's a logical error?
Jimmacle
Jimmacle2y ago
is what a logical error? i'm just saying your debugger works so i don't know why you're saying it doesn't
The king of kings
I've watched a tutorial on debugging and you can see multiple windows in your ide where you can check all the variables the methods and the values, I don't see the same here 🤷🏻‍♂️
The king of kings
I mean the bug surly is not a compiler error or syntax error, so it should be a logical error. I think I've seen this
Jimmacle
Jimmacle2y ago
all i'm asking you to do is start with the line that prints the message and work backwards to figure out what program state is leading to that message being printed then you can figure out if something is being done incorrectly it's not a compiler error, if it was your code wouldn't run
The king of kings
Ok! You're right. I've done that for the last hours and not sure what the problem is honestly.
Jimmacle
Jimmacle2y ago
so what steps did you take and where are you currently at? have you eliminated any possibilities?
The king of kings
Thank you for asking these questions First, I add two curly brackets and an default else to the last if statement. But it didn't make any different second I deleted the key part because if condition doesn't end up with a semicolon and I though this might be the issue
if (key.Length <= 0) key = "a";
if (key.Length <= 0) key = "a";
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server