Why does sorted "tan" != "ant"? [closed] - string

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to sort the characters in a string by sorting a slice of the bytes in the string (using sort.Slice). The code I'm using gets the right results sometimes but other times produces results I can't make sense of.
package main
import (
"fmt"
"sort"
)
func main() {
for _, s := range []string{"nat", "tan", "ant"} {
b := []byte(s)
sort.Slice(b, func(i int, j int) bool { return s[i] < s[j] })
fmt.Println(s, string(b))
}
}
https://play.golang.org/p/bC9QWq7aF3G
I would expect "nat", "tan" and "ant" to all be sorted to "ant", but "tan" is sorted to "atn".

Change your sort.Slice line to:
sort.Slice(b, func(i int, j int) bool { return b[i] < b[j] })
sort.Slice needs your less function to compare values in the slice in order to sort the way you intended. Your bug is that you used s rather than b in your less function.

Related

Converting string to float using Arduino [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
I am trying to convert the string in my code to float but using atof() for a sub string seems not be working. Advise on how to go about it appropriately will be appreciated.
It is for a BME280 sensor using 433MHz transmitter and receiver
void loop{
if(rfrx.recv(buff, &bufflen)){
String rxstr = String((char*)buff);
for (int i=0;i=rxstr.length();i++){
if (rxstr.substring(i,i+1) == ","){
String T = rxstr.substring(0,i);
String P = rxstr.substring(i+1);
String A = rxstr.substring(i+2);
String H = rxstr.substring(i+3);
break;
}
}
}
}
Have figured it out
The .toFloat()
Thanks

How to read two named inegers as a tuple in native Rust? [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 months ago.
Improve this question
In Python I can do this:
a, b = map(int, input().split(" "))
... to get exactly 2 integers from user input.
How can I achive the exact same result in Rust (get 2 integers as a named tuple) without any extern crates?
For the sheer fun of it, I translated your code as literally as possible to Rust:
// replace with some read from stdin
let input = "1 2";
let (a, b) = if let &[a, b] = &input.split(' ').map(|c| c.parse::<u32>().unwrap()).collect::<Vec<_>>()[..] {
(a, b)
} else {
panic!("ValueError: too many/few values to unpack (expected 2)");
};
println!("a = {}, b = {}", a, b);

strings.Contains in switch-case GoLang [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
Is it possible to use strings.Contains in switch case?
something like:
func function(str string){
switch str {
case "str1":
...
case strings.Contains("test"):
...
default:
...
}
}
Edit: its an example, thats not the real case I need it for. Im filtering many results and I want all results that contains X and I also have cases that I need to fully match.
You can do this:
package main
import (
"fmt"
"strings"
)
func main() {
str := "testString"
switch {
case strings.Contains(str, "test"):
fmt.Println(true)
default:
fmt.Println(false)
}
}
https://go.dev/play/p/_2fMd-3kE-r
switch without argument is like switch true.
Why do you need a switch for this boolean output?
Use the following inbuilt function.
func strings.Contains(s string, substr string) bool

If length is 2 do Haskell [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 1 year ago.
Improve this question
I'm new to Haskell and I'm not sure how to work around the If-Else, for example:
function str = if ((length str) = 2) then (....)
In java we would:
if (str.length =2){
str = "2"}
else { str ="1"}
How do you write it in haskell?
You can use Guards:
fnc :: String -> String
fnc s | length s == 2 = ...
| otherwise = ...
More to Guards
Or conditions
fnc :: String -> String
fnc s = if length s == 2 then ... else ...
It is also possible to use pattern matching, more here.
There are several ways to achieve conditions (e.g. case of) in Haskell.

Haskell Reversed Number Digit List [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new to Haskell and I am trying to turn an int into a reversed digit list(of ints).
What I have is:
Lnat 0 = [0]
Lnat x = [mod x 10] ++ Lnat (div x 10)
However I get the error "Not in scope: data constructor 'Lnat'" on both lines and it crashes trying to load the file.
Could you please explain the root of this and how to fix it?
All values must start with a lowercase character. If it starts with a capital or : then that value is a data constructor, to be used in data declarations. This is what you'll want to change your function to:
lnat 0 = [0]
lnat x = mod x 10 : lnat (div x 10)
Note that I also changed the inefficient ++ operator to : to add a bit more speed.

Resources