Question I can't find an error in the Java code

Nov 22, 2022
12
1
15
0
I wrote the source code for a new application on IOS, when writing there were no errors, but when I ran the code, an error appeared in this place:

Code:
 var t = readLine()!
var s = readLine()!
var len_s = s.count
var t_lis = Set(t)
var c_s   = Counter(s)
var c_t   = Counter(t_lis[len_s])
var c_res = [String: String]()
var summ  = 0
for e in c_s{
    c_res[e] = [c_s[e], min( c_s[e], c_t[e] )]
    summ += c_res[e][1]}
for i in (t.count-s.count) + 1 {
    if summ == len_s-1{
        print(i)
        break
        
    }
    if t[i] in c_res
            if c_res[t[i]][1] > 0{
            c_res[t[i]][1] -= 1
            summ -= 1
    }
    if i+len_s < t.count && t[i+len_s] in c_res
        if c_res[ t[i+len_s] ][1] < c_res[ t[i+len_s] ][0]{
            c_res[ t[i+len_s] ][1] += 1

Who can tell what is wrong?
And does anyone know a quality service for code analysis? Which could make one code unique across multiple projects. Thanks in advance
            summ += 1
        
    }
    else
    print(-1)
        }
 

Ralston18

Titan
Moderator
Reactions: Mandark
Nov 22, 2022
12
1
15
0
Hello, these are two different topics. That issue has been resolved, this is a new code for the editor program. I need to make it workable and unique so that there are no intersections.
 

Ralston18

Titan
Moderator
What is going wrong with the code? What is not happening as expected? What is happening that is unexpected? What error codes, if any, are being generated?

First : add some comments throughout your code to explain what each line, loop, or if/then logic is doing. Even if I do not understand the details of the code I should be able to read the comments and understand well enough what is being done and the underlying logic of the actions/code doing the work.

Second
: use descriptive variable names that clearly express what the variable is. Not just a simple letter or abbreviations such as "c" or "t". "c_s", or c_res.

Or at least comment in some explanation or definition of each variable at the start. I can infer that "c" represents a counter and a few other things as well.

"t" & "s" being ...... ?

For example: what is "e" in

"for e in c_s{
c_res[e] = [c_s[e], min( c_s[e], c_t[e] )]
summ += c_res[e][1]}"

?

"e" just sort of suddenly showing up. Does "e" need to be initialized/declared to some starting value?

Third: walk through your code line by line pretending that you are the computer. As variables are initialized and then later changed use a piece of paper to track the value of each variable. Do exactly as your code is written and the logic therein is applied.

Should only take a few passes through to discover where the editor program is going astray.

Commented code is much easier to follow and to troubleshoot. Put in extra comments during development and then edit, if and as necessary once all is working.

Also add some temporary lines of code that could, for example, print out/display the values of all variables. If some variable that should be increasing or changing in some manner suddenly goes to zero or even negative then that variable and how it is being processed can be addressed in detail.

One trick is to comment out those temporary lines once all is working. Leave the "comments" in place for future troubleshooting if necessary.
 
Reactions: Mandark
Nov 22, 2022
12
1
15
0
What is going wrong with the code? What is not happening as expected? What is happening that is unexpected? What error codes, if any, are being generated?

First : add some comments throughout your code to explain what each line, loop, or if/then logic is doing. Even if I do not understand the details of the code I should be able to read the comments and understand well enough what is being done and the underlying logic of the actions/code doing the work.

Second: use descriptive variable names that clearly express what the variable is. Not just a simple letter or abbreviations such as "c" or "t". "c_s", or c_res.

Or at least comment in some explanation or definition of each variable at the start. I can infer that "c" represents a counter and a few other things as well.

"t" & "s" being ...... ?

For example: what is "e" in

"for e in c_s{
c_res[e] = [c_s[e], min( c_s[e], c_t[e] )]
summ += c_res[e][1]}"

?

"e" just sort of suddenly showing up. Does "e" need to be initialized/declared to some starting value?

Third: walk through your code line by line pretending that you are the computer. As variables are initialized and then later changed use a piece of paper to track the value of each variable. Do exactly as your code is written and the logic therein is applied.

Should only take a few passes through to discover where the editor program is going astray.

Commented code is much easier to follow and to troubleshoot. Put in extra comments during development and then edit, if and as necessary once all is working.

Also add some temporary lines of code that could, for example, print out/display the values of all variables. If some variable that should be increasing or changing in some manner suddenly goes to zero or even negative then that variable and how it is being processed can be addressed in detail.

One trick is to comment out those temporary lines once all is working. Leave the "comments" in place for future troubleshooting if necessary.
Thanks, another question. I want to use this code for two applications, can I make two unique ones from one code?
 

Ralston18

Titan
Moderator
Not sure about your question per se.

However, it is not unusual to clone some piece of code and then edit or modify that code for use in another application.

Or reuse an application for another purpose whose requirements parallel the original app. Many science, math, and engineering formulas are similar, if not identical.

Depends on the overall requirements.

In some cases, if the required piece of code is consistent enough and well vetted, the code could be used as function that is simply plugged into each application where necessary.

Excel is a good example: There are many financial and other functions built in that allow the end user to call the function, provide the input requirements, and get the results. No need to construct some new formula that could be cumbersome and awkward. Statistical functions come to mind. Again, no need to reinvent those "wheels". Unless there is some very unique aspect about computational or other processing requirements.

Very good chance that you can find code online for just about any project. Possibly for direct use or more likely as a reference template.

Then modify and tweak the template to meet the requirements.

On the other hand, it is all too easy to get bogged down in the modification process while trying to tweak things to work. May be much more straightforward and easier to just start from scratch. Depends on the code, the complexity, and how well the code is or is not documented.

Similar to construction projects where a building is simply demolished and a new building is constructed. Versus trying to fix and modify the original structure.

Old code, tweaked/modified code, new code.... What becomes important is that the code can be tested and proven to meet the functional requirements.

All too often skimped on nowadays.....
 
Nov 22, 2022
12
1
15
0
Not sure about your question per se.

However, it is not unusual to clone some piece of code and then edit or modify that code for use in another application.

Or reuse an application for another purpose whose requirements parallel the original app. Many science, math, and engineering formulas are similar, if not identical.

Depends on the overall requirements.

In some cases, if the required piece of code is consistent enough and well vetted, the code could be used as function that is simply plugged into each application where necessary.

Excel is a good example: There are many financial and other functions built in that allow the end user to call the function, provide the input requirements, and get the results. No need to construct some new formula that could be cumbersome and awkward. Statistical functions come to mind. Again, no need to reinvent those "wheels". Unless there is some very unique aspect about computational or other processing requirements.

Very good chance that you can find code online for just about any project. Possibly for direct use or more likely as a reference template.

Then modify and tweak the template to meet the requirements.

On the other hand, it is all too easy to get bogged down in the modification process while trying to tweak things to work. May be much more straightforward and easier to just start from scratch. Depends on the code, the complexity, and how well the code is or is not documented.

Similar to construction projects where a building is simply demolished and a new building is constructed. Versus trying to fix and modify the original structure.

Old code, tweaked/modified code, new code.... What becomes important is that the code can be tested and proven to meet the functional requirements.

All too often skimped on nowadays.....
I was advised a service that makes code unique is called AppRefactoring. Have you heard about him?
 

Ralston18

Titan
Moderator
AppRefactoring. Not sure about what is meant by "makes code unique"(?).

Overall, just a general awareness of the concept.

However, I ran a couple of online searches and basically came away with the impression that a lot of the promotion is mostly fluff.

Much marketing speak. Diagrams, buzzwords, and so forth.

Yes, code needs to be kept up to date and rewritten in part and in full to take advantage of changing technology, improve performance, be more secure, facilitate debugging, clean up documentation, etc. etc..

Again: there are tradeoffs to be made: edit/re-edit continuously or, at some point just a "do-over" or rewrite of the code.

Either way, the development process still requires proper thought and control with respect to requirements, design, coding, documenting, testing, and implementation. Configuration Management being key as well.

Basics.

Early programmers had an expression that I rarely see or hear any more.

"GIGO".

"Garbage In, Garbage Out."
 
Nov 22, 2022
12
1
15
0
AppRefactoring. Not sure about what is meant by "makes code unique"(?).

Overall, just a general awareness of the concept.

However, I ran a couple of online searches and basically came away with the impression that a lot of the promotion is mostly fluff.

Much marketing speak. Diagrams, buzzwords, and so forth.

Yes, code needs to be kept up to date and rewritten in part and in full to take advantage of changing technology, improve performance, be more secure, facilitate debugging, clean up documentation, etc. etc..

Again: there are tradeoffs to be made: edit/re-edit continuously or, at some point just a "do-over" or rewrite of the code.

Either way, the development process still requires proper thought and control with respect to requirements, design, coding, documenting, testing, and implementation. Configuration Management being key as well.

Basics.

Early programmers had an expression that I rarely see or hear any more.

"GIGO".

"Garbage In, Garbage Out."
Okay, thank you!
 

ASK THE COMMUNITY

TRENDING THREADS