Is it humanly possible to fix indent size issues w/o access to a syntax tree? [closed] - whitespace

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 7 years ago.
Improve this question
Asked another way, if I showed you this masked code file, using only your human brain, is it possible to fix the indentation issues, even if you know it should be 2-space indentation?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x
x
xxxxxxxxxxxxxxxxxxxxxxxxxxx
I have my own ideas, but I don't want to bias the answer. The actual source code and language will be revealed after I get a good batch of answers. Feel free to post your fix as a code block below.
This test assumes the following:
You have no idea the language in which this code is written.
All you know is how many spaces or tabs lead up to the first character of each line. In this case, there are no tabs (just spaces).
You know what the indent size should be. In this case, 2 spaces.
Note: If it's possible with your human brain, it should also be possible with code, right?
Bonus Points (optional): How would you break-down the logic to tackle this problem?
EDIT: Here's the source code, from which these exes were created:
function greet(firstName, lastName) {
var firstName = prompt('What is your first name?');
var lastName = prompt('Last name?');
var fullName = firstName + ' ' + lastName;
for (var i = 0; i < 10; i++) {
console.log('Hello,', fullName + '!');
}
}
greet(firstName, lastName);

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x
x
xxxxxxxxxxxxxxxxxxxxxxxxxxx

Related

Elegant/Best solution to get the last element from an iteration [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm receiving multiple messages over a channel, and after iterating over them, I would like to keep the last element for further usage. My first (probably bad!) approach was to declare some variable, and then assign it every loop.
let last = 0;
for some in rx_from_channel.iter() {
let last = some;
}
let a = last + 5;
I really don't like this solution - is there a to avoid assigning last in each loop?
Further, I would have expected that after using let last inside the for {} loop for the first time, the variable declared above the loop goes out of scope - and ļast shouldn't be available after the for {} loop at all. The compiler suggests otherwise - why?
You can just do:
let last = rx_from_channel.iter().last().unwrap_or_else(|| &0);
let a = last + 5;
See last()
fn last(self) -> Option<Self::Item>
Consumes the iterator, returning the last element.
Doesn't method last() solve your problem?

Calculate difference between two numbers and get the absolute value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to find the difference between two numbers in Go and the result should not be in "-".
Please find my code below:
dollarValue := 240000 - 480000
The result is "-240000". But my expected output is just "240000". Can anybody help on how to calculate the difference between these two numbers.
Your title is misleading. It should be states without negative instead of - operator.
Basically what you want to get is the absolute different between two numbers
You have two options:
Use if/else condition to return the positive result if the result is negative
Use math.Abs (need to convert from/to float)
Just implement your own method
func diff(a, b int) int {
if a < b {
return b - a
}
return a - b
}
and use it like this:
dollarValue := diff(240000, 480000)

Regex dynamic string manipulation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im aiming for a regex formula to return chunks of a string based on a character, if this string contains L1 then its going to be only one chunk, if L2 is found it would return 2 chunks, L3 = 3 chunks.
Example
Lets assume we have this string
"L2N1N1"
and we would like to get 2 string
"L2N1" and "L2N1N1"
Another example
"L3N1N1N2"
to return 3 strings
"L3N1" "L3N1N1" "L3N1N1N2"
Im using Ruby
"L3N1N1N2".sub(/L(\d)(?:N\d)+/) do |m|
$1.to_i.times.map { |i| m[0..3+2*i] }.join(' ')
end
#⇒ "L3N1 L3N1N1 L3N1N1N2"

Matching partial strings in a phrase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I put up an intranet site that loops through a .csv dump of our customer database and uses a form to help look up account numbers.
I want to treat all of my keywords as wild card terms, but respect their order. For example, if I have company A: "The Monogramme Shoppe" and company B: "Monograms & More at The Shop", I want to return A and B options if I type "mono shop" in the form field. This code does that:
company_lookup = company_lookup.split(" ")
counter = company_lookup.length
company_lookup.each do |com|
if company.downcase.include? com.downcase
counter = counter - 1
end
end
if counter == 0
match_found = true
account_number = row[2].to_s
matches.push [account_number, company]
end
But if I type "mono the", I also get both results. There, I only want to get the B result.
Is there any way to use regular expressions to, say look for PartialKeyword1 and PartialKeyword2 in a string and return true if matched?
You can use the following code to construct a regular expression to match the company name, and then use this regular expression to find the matched record.
company_lookup = company_lookup.split(" ").map{|r| Regexp.quote(r)}.join('.*?')
if company =~ /#{company_lookup}/i
matches.push [row[2].to_s, company]
end
If performance is a big concern, or the data size is huge, you'd better try some full text search engine, such as Thinking Sphinx

Sorting arbitrary numeric data and leave whitespaces for the missing values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to sort the numeric data in a row/column while leaving space of missing values at scale of 1-5.
Like:
A B
13245 1_2_3_4_5
152.... 1_2_ _ _5
Try this UDF:
Function ModSort(Str As String) As String
Res = ""
For Iter = 1 To 5
If InStr(1, Str, CStr(Iter)) Then
Hold = CStr(Iter) & "_"
Else
Hold = " _"
End If
Res = Res & Hold
Next Iter
Res = Left(Res, 9)
ModSort = Res
End Function
Screenshot:
Let us know if this helps.
Not meant as a serious answer (UDF seems good enough) but appears possible with a formula:
=IF(LEN(SUBSTITUTE(A2,1,""))<LEN(A2),"1_"," _")
&IF(LEN(SUBSTITUTE(A2,2,""))<LEN(A2),"2_"," _")
&IF(LEN(SUBSTITUTE(A2,3,""))<LEN(A2),"3_"," _")
&IF(LEN(SUBSTITUTE(A2,4,""))<LEN(A2),"4_"," _")
&IF(LEN(SUBSTITUTE(A2,5,""))<LEN(A2),"5",)

Resources