初学者js常见问题
When I started on learning how to code I also was struggling to come up with an algorithm to solve it and I didn’t even know how to start. I often thought that this was not my piece of cake. In the following article I will tell you why you may be struggling to crack a problem that I myself faced when I started to code and now as a Professional Software Engineer have seen people why they give up too easily on finding a solution.
当我开始学习如何编码时,我也很努力地想出一种算法来解决它,我什至不知道如何开始。 我常常以为这不是我的小菜一碟。 在下一篇文章中,我将告诉您为什么您可能难以解决我自己开始编写代码时遇到的一个问题,而现在作为一名专业软件工程师,人们看到了为什么他们在寻找解决方案时会轻易放弃的人。
1.获得帮助 (1. Getting Help)
No matter how hard is the problem, don’t get help !!. Try out different approach or see the problem again and try to understand it. This may seem contrary to giving advice but once you try to solve problems on your own this will boost your confidence and you will have an arsenal of weapons which you can use to solve different problems. So as you solve more problems on your own you will get motivated and will accept a harder challenge to solve next time. If you get help and get the solution without ever thinking about it, the solution will only give you solace and next time when you solve a different problem and get stuck you will have a temptation to seek help. You will be less interested in solving a new problem as you know that you were not able to solve the prior one or were able to solve only a handful that you ever tried.
无论问题有多严重,都不要寻求帮助! 尝试使用其他方法或再次查看问题并尝试理解它。 这似乎与提供建议相反,但是一旦您尝试自己解决问题,这将增强您的信心,并且您将拥有可以用来解决不同问题的武器库。 因此,当您自己解决更多问题时,您会充满动力,并且会接受更艰巨的挑战来下次解决。 如果您获得帮助而没有考虑解决方案,那么解决方案只会给您带来安慰,而下次当您解决其他问题并陷入困境时,您会很想寻求帮助。 您将不会对解决新问题感兴趣,因为您知道自己无法解决先前的问题,或者只能解决少数尝试过的问题。
I am not saying that you should never get help, instead give your 100% in solving problem and assume that there is nobody that is going to help you. In this way, you will be a good coder in no time.
我并不是说您永远都不会获得帮助,而是要100%地解决问题,并假设没有人会帮助您。 这样,您将很快成为一名优秀的编码人员。
2.仅坚持一种解决方案 (2. Sticking to only one solution)
Suppose you started out with a potential solution using Arrays but the solution using arrays is too complicated, then you can try using for example dictionaries, HashMaps, etc. or you can use a different approach using arrays also. The important thing is to be aware while working out with algorithm is that whether your algorithm is getting complex and while coding it will get even tougher. One way is to ask yourself this question even if you have a solution. Can I do better?
假设您从使用数组的潜在解决方案开始,但是使用数组的解决方案太复杂了,那么您可以尝试使用字典,HashMaps等,也可以使用其他方法使用数组。 重要的是要在使用算法时要知道,您的算法是否会变得复杂,而在编码时会变得更加困难。 一种方法是即使有解决方案,也要问自己一个问题。 我可以做得更好吗?
问题 (Problem)
Write a program to check it given string is “Palindromable”or not.
编写程序以检查给定的字符串是否“ Palindromable”。
What is a “Plaindromable” string?
什么是“ Plaindromable”字符串?
Let’s say you are given a string. You can get many strings (combination) out of the given original string if you rearrange characters of original string.
假设您得到一个字符串。 如果重新排列原始字符串的字符,则可以从给定的原始字符串中获得许多字符串(组合)。
String is “Palindromable” if any one combination is palindrome.
如果任何一种组合是回文,则字符串为“ Palindromable” 。
Example 1:
范例1:
Original String: NINIT
原始字符串:NINIT
Combinations: NINIT, NNIIT, IINNT,ININT, IITNN, NITIN, INTIN, INTNI, NTNII, NNTII and so on
组合:NINIT,NNIIT,IINNT,ININT,IITNN,NITIN,INTIN,INTNI,NTNII,NNTII等
Original string is Palindromable because two palindrome can be made out of it.
原始字符串是可回文的,因为可以从中产生两个回文。
Example 2:
范例2:
Original String: NINNIT
原始字符串:NINNIT
Combinations: NINITN, NNINIT, IINNNT, INNINT, INITNN, NNITIN, INTNIN, INTNIN, NTNINI, NNTINI and so on
组合:NINITN,NNINIT,IINNNT,INNINT,INITNN,NNITIN,INTNIN,INTNIN,NTNINI,NNTINI等
Original string is NOT Palindromable because NO palindrome can be made out of it.
原始字符串不可回文,因为无法使用回文。
使用HashSet的第一个解决方案的伪代码 (Pseudo Code for first solution using HashSet)
function palindromable(String inputString) Create a character HashSet charSet Convert inputString to character Array strArray using toCharArray for each character 'c' in strArray if 'c' is present in charSet Remove 'c' from charSet else Add 'c' to charSet End of if End of loop if charSet size is greater than 1 // if there is odd number of occurence for more than 1 character
return false else return true End of loopEnd of function
使用数组的第二个解决方案的伪代码 (Pseudo code for 2nd Solution using Array)
function isPalindrome(String inputString)Create a tempArray for storing single occurences of the characters in inputStringintialize index to zerointialize count to zerointialize oddcount to zerointialize flag to falseConvert inputString to character Array strArray using toCharArrayif strArray length is equal to one return trueEnd of iffor each character 'c' in strArray for each character 'j' in tempArray if 'c' is equal to 'j' set flag to true break End of if End of loop if flag is equal to false set count to zero for each character 'k' in strArray if 'c' is equal to 'k' increment count by 1 End of loop if count divided by 2 does not give remainder 0 increment oddcount by 1 End of if if count divided by 2 does not give remainder 0 and length of the string is a multiple of 2 return false End of if if count divided by 2 does not give remainder 0 and length of the string is not a multiple of 2 if oddcount is greater than 1 return false End of if End of if tempArray[index] = 'c' increment index by 1End of loopreturn trueEnd of function
You can clearly figure out First Solution is much more efficient than the Second Solution without getting into the time complexity of both of these algorithms, also you’ll require much more time to come up with the second algorithm. Even if you have a solution or you get a feeling that this algorithm will get more complicated then ask yourself, Can I do better?
您可以清楚地看出,第一种解决方案比第二种解决方案有效得多,而无需考虑这两种算法的时间复杂性,而且您将需要更多的时间来提出第二种算法。 即使您有解决方案,或者感觉到该算法将变得更加复杂,请问自己: 我能做得更好吗?
3.不知道如何调试 (3. Don’t know how to debug)
![Debugging the Code](https://i-blog.csdnimg.cn/blog_migrate/78a912be9238415796c60dc291e72218.png)
Let me tell you I didn’t use debuggers for a long time, it feels complicated to use but it has many perks. Once you start using debugger you’ll know where the mistake is and what exactly it is, in your code sooner. When you use IDEs for example of VSCode, Eclipse for coding, they have debugger tools. Most of the time new programmers don’t use these debuggers. One mistake you might be doing is to just see the output and getting stuck because you don’t see the right result/output and don’t know what to do next.
让我告诉你,我很长时间没有使用调试器了,使用起来感觉很复杂,但是有很多好处。 一旦开始使用调试器,您将更快地在代码中知道错误的出处和确切的含义。 当您使用VSCode这样的IDE(例如Eclipse)进行编码时,它们具有调试器工具。 大多数时候,新程序员不会使用这些调试器。 您可能会犯的一个错误是,仅看到输出并陷入困境,因为您看不到正确的结果/输出并且不知道下一步该怎么做。
So next time you think that your code is not working properly then remember to debug. By using breakpoints and analyzing the values in the variables and also the state of the object you can easily find out what part of the algorithm needs to be changed, this can save you a lot of time and you can be more efficient. So first learn how to debug on your IDE.
因此,下次您认为您的代码无法正常工作时,请记住进行调试 。 通过使用断点并分析变量中的值以及对象的状态,您可以轻松地找出需要更改算法的哪一部分,这可以节省大量时间,并且可以提高效率。 因此,首先学习如何在IDE上进行调试。
4.立即开始编码 (4. Start coding straight away)
This is by far the most mistake beginners do. When you are trying to solve a problem don’t just directly jump into coding. Take a pen and paper and figure out the solution instead of just hitting the keyboard directly. This will save you a lot of time because your logic would be more clear and you’ll make fewer mistakes while coding.
到目前为止,这是初学者最容易犯的错误。 当您尝试解决问题时,不要只是直接跳入编码。 用笔和纸找出解决方案,而不仅仅是直接敲击键盘。 这将为您节省大量时间,因为您的逻辑会更清晰,并且在编码时会减少错误。
You can follow the steps below to develop an algorithm.
您可以按照以下步骤开发算法。
步骤1.为自己创建一个示例。 (Step 1. Work an example for yourself.)
步骤2.写下您的工作。 (Step 2. Write down what you did.)
步骤3.概括步骤。 (Step 3. Generalize steps.)
From the above steps, you’ll get the pseudo code for your algorithm. The next step is coding! If you follow these steps you’ll arrive at the solution very much early. Or you don’t even have to follow these specific steps, when you see a problem forget about coding and syntax, just think logically and come up with a simple solution for example maybe brute-force approach then build and improvise your algorithm.
通过以上步骤,您将获得算法的伪代码。 下一步是编码! 如果您按照这些步骤操作,您将很早就找到解决方案。 或者,您甚至不必遵循这些特定步骤,当您发现忘记编码和语法的问题时,只需逻辑思考并提出一个简单的解决方案,例如蛮力方法,然后构建并改进您的算法。
When you have an algorithm at your hand remember to evaluate your algorithm’s time/space complexity and ask Can I do better?
当手头有一个算法时,请记住评估算法的时间/空间复杂度,并询问我可以做得更好吗?
5.解决不充分 (5. Not solving enough)
There are many resources on the internet that you can make use of to level up your coding game. Pump up your brain muscles by solving more and more problems and trying out different solutions for the same problem this way in job interviews you’ll never be nervous about not being able to come up with an algorithm. You’ll know just by seeing the problem which data structures to use and which programming approach to use. You’ll be confident that whichever problem the interviewer will throw at me, I can find a solution.
互联网上有许多资源可以用来升级编码游戏。 通过解决越来越多的问题并以此方式在面试中为同一问题尝试不同的解决方案来增强大脑的肌肉,您永远不会担心无法提出算法。 您只要看到问题就可以知道要使用哪种数据结构以及要使用哪种编程方法。 您将确信,无论面试官会问我什么问题,我都能找到解决方案。
Following are some of the resources that you can make use of to level up your coding game.
以下是一些您可以用来升级编码游戏的资源。
翻译自: https://medium.com/swlh/5-common-mistakes-made-by-beginners-who-are-learning-to-code-4b6edf5ddce5
初学者js常见问题