Return number as text [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I convert an integer into its verbal representation?
I am looking for some solution to return a number as text.
By that I mean that 100 should be returned as 'one hundred'
It is more or less just a tidious task to make such a function myself, but I rather not re-invent the wheel, and this can't be the first time someones has requested this.
Unfortunatly my search so far has not turned up anything, so here I try stackowerflow.
Basically the numbers comes from a database, so if there is some smart methods you could use here it would be pretty nice.
As mentioned, a small function that returns eg. 100 as 'one hundred' is not a complicated task, but what if you need to take language considarations into the solution?
Has anybody come accross something that actually can do this, and perhaps in multiple languages?

Yes you can use this:
Java numbers to text
Basically the idea is to form the numbers by simply defining all the digits and the tenths, and after that you can also the define the hundreds, the thousands and so on. Because numbers in English are always formed the same way, this is very easy for the English language.

Something like this (may be not the best one, I just make a draft search)?
http://www.daniweb.com/software-development/csharp/threads/53072

Related

How to print a text using "pixels" [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm using a fitness tracker with a watchface store - some of this watchfaces print the current time using other elements than simple text.
A few months ago I tried to beat a challange in "Coding game" where you have to print dynamic text in ascii art - I didn't manage to do that :(
I'm assuming that the basic technique should be the same but I cannot think about how to do it. May anyone tell me how to print e.g. the current time using dots / squares?
My last approach was to define a two dimensional array for each possible character, bitwise defining which spot should be active or inactive. But as this is pretty hard work I discarded that really quick.
Thanks for your help.
Actually, you were on the right track. That's one way to do it - a 2D array for each character with the bitmask. Or better yet - a full ASCII picture. Takes some work upfront, but not that much - you can probably do it in a day or two. You can also simplify the typing a bit. Like this:
Dictionary<char, string[]> = {
{'A', new string[] {
" AAAA ",
"AA AA",
"AAAAAA",
"AA AA",
"AA AA"
}},
// etc. for all characters in your "ASCII font"
}
Another way would be to define some sort of "vector format" for your characters, scale it to be as big as you need, and then convert to ASCII with some sort of algorithm that uses the characters -|/\. for outlines. That's much harder, and you still need to describe every character individually. But it looks prettier.
Or take a look at this answer: https://stackoverflow.com/a/7239064/41360 - that actually uses a bitmask. Yes, it probably took someone a few days to get it right. Harder than the string approach above, but also more compact if you're short on space (like in some embedded hardware).
And then there's Figlet - that piece of software has gone all the way and defined its own font format, not to mention some sort of advanced text rendering engine which actually merges the characters together. Crazy shit.
But no matter what approach you use, someone somewhere will need to design each letter individually and store the designs in some kind of format (thus producing a "font"). Most likely it will be you. There's no way around it.
Programmers might be lazy and use tricks to make their lives easier - but sometimes hard work is simply unavoidable. When that comes, you just do it. At the end of the day, it's the result that counts, not how it was obtained (well, as long as it's ethical/legal of course).

Replacing string in a list doesn't work [duplicate]

This question already has answers here:
Why does the Replace() string method not modify my string variable?
(4 answers)
Closed 9 years ago.
I cannot, for the life of me, understand why the Replace(); method will not work when I try to replace the text of an element inside a list of type String. This question is, to an extent, related to a previous thread of mine [found here: Merging two files and handling duplicate entries (would be a good idea to view it, as well, for the full code)], but at the end of the day, it all boils down to the fact that the command doesn't work while, my code, is actually correct (!).
I'll give you an example of what doesn't work in my situation. In its most basic form, for the sake of example, this is what I'm trying to accomplish but it doesn't work (for real!):
[In my original code, I've triple-checked (with breakpoints and everything) that my list indeed contains the string I want to replace (I'm replacing the element itself!), but it just won't do it! Seriously now, it is a 4 element list at the present time (although more elements CAN be added, refer to other thread).]
One last thing, sorry about the punctuation (too many exclamation marks, I know), but I'm actually raging over this. Code below (remember, most basic form, but an example I tried):
// List[index].Replace(oldValue, newValue);
newFile[3].Replace(newFile[3], "Replace it with this!");
Could you help me with this?
newFile[3] = newFile[3].Replace(newFile[3], "Replace it with this!");
In c#, strings are immutable. As such, the Replace method returns a new string.
Edit:
The main reason for strings to be immutable is to make them thread-safe.
If you wanna find out more, have a read: Why .NET String is immutable?
newFile[3] = newFile[3].Replace(newFile[3], "Replace it with this!");

To find out the number of occruence of words in a file

I came across this question in an interview:
We have to find out the number of occurences of two given words in a text file with <=n words between them.
Example1:
text:`this is first string this is second string`
Keywords:`this, string`
n= 4
output= 2
"this is first string" is the first occurrence and number of words between this and string is 2(is, first) which is less than 4.
this is second string is the remaining string. number of words between *this and string * is 2 (is, second) which is less than 4.
Therefore the answer is 2.
I have thought that I will use
Dictionary<string, List<int>>.
My idea was that I use the dictionary and get the list of places where the particular word is repeated and then iterate through both the lists, increment the count if a condition is met and then display the count.
Is my thinking process correct? Please provide any suggestions to improve my solution.
Thanks,
Not an answer per-se (as quite honestly, I don't understand the question :P), but to add some general interview advice to the other answers:
In interviews the interviewer is always looking for the thought process and that you are a critical, logical thinker. Not necessarily that you have excellent coding recall and can compile code in your brain.
In addition interviews are a stressful process. By slowing down and talking out loud as you work things out you not only look like a better communicator and logical thinker (even if getting the question wrong), you also give yourself time to think.
Use a pen and paper, speak as you think, start off from the top and work through it. I've got jobs even if I didn't know the answers to tech questions by demonstrating that I can at least try to work things out ;-)
In short, it's not just down to technical prowess
I think it depends if the call is done only one or multiple times per string. If it's something like
int getOccurences(String str, String reference, int min_size) { ... }
then you don't really need the dictionary, not even a ist. You can just iterate through the string to find occurrences of words and then check the number of separators between them.
If on the other hand the problem is for arbitrary search/indexing, IMHO you do need a dictionary. I'd go for a dictionary where the key is the word and the value is a list of indexes where it occurs.
HTH
If you need to do that repeatedly for different pairs of words in the same text, then a word dictionary with a list of indexes is a good solution. However, if you were only looking for one pair, then two lists of indexes for those two words would be sufficient.
The lists allow you to separate the word detection operation from the counting logic.

Retrieving the highest number in an array recursively in C#?

How can I retrieve the highest number in an array recursively in C#?
Right now you're probably thinking that we're mean for not giving you the answer -- and I admit that I have the answer written down and part of me wants to give it to you, even.
Programming is all about finding the solutions to problems yourself. When you're hired as a programmer, you may have other people to lean on, but they've all got their own problems, and you'll need to be able to pull your own weight.
Recursion (in an oversimplifed answer) means to call the same operation over and over until the result is produced. That means you need in every recursive operation, you need to know (at least) two things:
What you're looking for
What you've found so far
The 'What you're looking for' is the termination condition. Once you find that, all work can stop and you can go home.
The 'what you've found so far' is how you know what've you've checked so you don't retread old ground.
So what do you need to know in order to find the highest value in an array recursively?
The contents of the Array.
The highest number you've found so far.
Have you already looked at this part of the Array? (Why look through it again?)
That would produce a method signature that looks like:
public int GetHighestNumber(int[] array, int highestNumberFound, int lastIndexChecked);
Once you're inside the array, you've got to do the following:
Iterate through the array
Stop when you find a value that is higher than the highestNumberFound
Call GetHighestNumber again with the new highestNumberFound and lastIndexChecked updated.
When there are no more 'higher' numbers, then return the highest number found.
I realize it sounds trite, but learning this stuff on your own will make you a better programmer.
If you want to be a professional programmer, you have got to learn this stuff on your own.
If you don't want to be a professional programmer, then drop the course and do something you love.
Here's just a hint (taking int[] as an example):
public int FindMax(int[] array, int indexSoFar, int maxSoFar)
Think about:
The start conditions
The termination conditions
How you move through the array recursively
Reason of EDIT: Didnt want to spoil the answere.
Greetings.

GENERAL: Programming Code Guidelines & Styles [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I know that each programming language has certain guideline and styles. My question is about two languages that I write code in, that isn't very popular or documented.
I know this topic is very broad, and everyone has their own unique way of doing things. What I would like is to hear advantage, disadvantages to certain styles.
In order to explore this question, imagine you are writing your own programming language, based on what you've experienced in the past, what is the best way of going about things?
Remember, there may be ups and downs based on specific languages, so think if this language didn't matter. I am still fresh to programming, so I want to get the best habits of making my code readable and easy to follow.
There are so many topics to talk about, Ill get run by the basics:
Global Variables
Should they start with _ and be all capitalized?
Local Variables
Should they end with _ and be always be lowercased?
Variable Names
If I am defining something like an employee's hourly wage, should it be EmployeeHourlyWage, Employee_Hourly_Wage?
Variable Types
Should you include the type of variable it is in the name, for example if I define $Hours and it has stored to it an integer, should I name it $Hour_INT so that I know when referring to it what type it is? Who knows, I might have an $Hours_FLOAT
Curly Brackets
Should the brackets line up with themselves such, the words, or what? Which one of these are best, preferred, most readable?
IF ($Test) {
//code
} ELSE {
//code
}
IF ($Test)
{
//code
} ELSE {
//code
}
IF ($Test)
{
//code
}
ELSE
{
//code
}
Alignment
I am constantly lining up variables and their values so I have an idea where what goes where. Is this bad practice:
// Assuming GUI(TOP, LEFT, HEIGHT, WIDTH)
GUI( 23 , 44 , 245 , 2323 )
GUI( 232 , 4332 , 22 , 6576 )
GUI( 21 , 4 , 1 , 5 )
GUI( 34235 , 13 , 31237 , 4564665 )
// OR
GUI(23,44,245,2323)
GUI(232,4332,22,6576)
GUI(21,4,1,5)
GUI(34235,13,31237,4564665)
Indenting
Why do some coders use spaces instead of tabs? is there a amount of spaces that is recommended?
I understand all of these could be questions in their own. I am not sure where to get all this knowledge from? I could spend hours just asking you what is the best method. I am sure the more college courses I take, the more it will be hit on (or not).
It would be awesome if there was a site where programmers of all kinds talk/discuss/rate/wiki the best methods and practices of programming. Would also help serve future languages to better suit the needs. I guess if there was one right way, there wouldn't be so many variations in languages and style. I just would like to know your arguments and whats mainstream so my coworkers know what I am coding.
These are all really subjective issues - people mostly disagree about these sorts of things, and to be honest it really doesn't matter that much! :-)
I'd say that the only thing that you can actually do wrong is to be inconsistent about whatever pattern you do use.
I think this is all a matter of personal taste.
Use the coding-style you feel conformtable with, whenever you can.
When you work on a team, try to all use the same coding-convention unless you want the code to be a mess.
And if you really don't know what convention to choose, choose a popular one: whether it is google's, Richard Stallman's or whoever's don't matter: just try to be consistent.
Mine evolved during the past few years and probably so will yours.
Here is my advice: you should focus on writing good and maintenable code first; tools exist to fix/change coding-styles.
Have a common standard within your team and follow within your team consistently. Make sure that it is readable and understandable. After all the code for us humans to read.. :) Computers use object code anyway..
Follow those and have it documented.
It's enough if the Code is understandable.
If a new joiner can able to understand your code based on the Document you provided, then you are good at it.
I think it's just more than enough. It's my opinion though.. :)
For Java, most people seem to follow a style that's based on the defacto standard described in this document: Code Conventions for the Java Programming Language.
For variable names, that means use camel case, starting with a lower-case letter, for example: employeeHourlyWage. For static final values (constants), use all-uppercase and separate words by underscores, for example: EMPLOYEE_HOURLY_WAGE.
Don't start global variables with an underscore, don't end local variables with an underscore. Do not include some abbreviation of the variable type in the name of the variable (doing that is called Hungarian notation).
I prefer the following bracket style:
if (condition) {
// do something
}
else {
// do something else
}
Indenting with 4 spaces is most common in Java.
I agree with the other answers that consistency is more important than what particular style you choose. For Java, it's a good idea to pick a style based on the above-mentioned document, as that will be what most Java coders will expect, and the style that's used in the standard Java library and almost all open source Java libraries.
For C, the GNU coding standards are probably a good thing to look at.
Here are my thoughts. Others will disagree, but as I think it has been pointed out, the main thing is to be consistent.
Global variables: I have no special naming convention for them, mainly because I avoid using them as much as possible. In my C code, I do use variables with static scope i.e. visible in the compilation unit, but again I don't bother with any naming convention.
I tend to use the Java capitalisation convention i.e. instances, variables, members etc use camelCase with a lower case first letter. Classes, structs, typedef types use CamelCase with a capitalised first letter.
I avoid prefixes and suffixes of any kind. In fact, I loathe prefixes ever since I had to debug a C++ class which had about twenty instance variables, all of which began "m_lpsz" and were thus almost impossible to distinguish. I know Charles Simonyi is a genius, but he should be shot for Hungarian notation.
Prefixes and suffixes that give you information about a variable are also unmaintainable. You need to be able to change the type of a variable or the scope of a variable without having to do a global search and replace on its name because you can guarantee that one day somebody in a rush will do the one without the other and then the variable is lying about what it is.
Braces: I think that if K&R 1st edition had not used the K&R brace style and somebody tried to introduce it now, we would regard them as insane. I can just about see a justification for it when terminals had 25 lines (or were actually teletypes with printed output), but there's no reason to use that style anymore. I find it makes the code look dense and cluttered.
Alignment: your first example is easier to read. What do you think the answer is?
Spaces, tabs: I use tabs because it's quicker to type 1 tab than 4 spaces. I think I'm probably in the wrong on this one, but on the other hand, with code reformatters, the issues that tabs cause (some people set the tab stops every four, some every 8 spaces) can be rectified quickly.
I think the best you can do is try out a few well documented coding style, choose what you like the most then use it consistently.
There are a lot of styles: http://en.wikipedia.org/wiki/Indent_style
Maybe K&R based styles the most popular, so would't be a bad idea to try one of those.
My personal choice is KNF.

Categories

Resources