How to parse int value from string in Rust [duplicate] - iterator

This question already has answers here:
Convert a String to int?
(8 answers)
How to convert a Rust char to an integer so that '1' becomes 1?
(4 answers)
Closed 4 years ago.
I'm trying to implement a network card game and I need to parse a string and get the rank value as a u32. The string looks like this
let response: String = "(6, Hearts)".to_string();
I've tried using:
let rank = response.chars().nth(1).unwrap() as u32;
with this I get a rank of 54 instead of 6 I think because it's returning a byte index instead of the actual value. I've also tried this:
let rank: u32;
response.chars()
.find(|x|
if x.is_digit(10) {
rank = x.to_digit(10).unwrap();
}
);
println!("rank {}", rank);
For this one I'm getting a mismatch type error on the closure
Compiling playground v0.0.1 (file:///playground)
error[E0308]: mismatched types
--> src/main.rs:12:35
|
12 | if x.is_digit(10) {
| ___________________________________^
13 | | rank = x.to_digit(10).unwrap();
14 | | }
| |_________________^ expected bool, found ()
|
= note: expected type `bool`
found type `()`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
Here's a link to the code: https://play.rust-lang.org/?gist=120b0ee308c335c9bc79713d3875a3b4&version=stable&mode=debug

Related

Random errors when matching strings [duplicate]

This question already has an answer here:
Karate - match fails for inline array
(1 answer)
Closed 1 year ago.
Usually matching works just fine but I've had weird random matching errors recently :
$.userInfo | not equal | match failed for name: 'phone' (MAP:MAP)
{"givenName":"##string","familyName":"##string","phone":"##string"}
{"givenName":"Andreas","familyName":"Conroy","phone":"+19515554384"}
$.userInfo.phone | not equal (STRING:STRING)
'##string'
'+19515554384'
How is '+19515554384' not a string ?
Thanks
Sounds like you have your "expected" vs "actual" data mixed up. Here below are 2 lines you can cut and paste and verify that it works:
* def response = {"givenName":"Andreas","familyName":"Conroy","phone":"+19515554384"}
* match response == {"givenName":"##string","familyName":"##string","phone":"##string"}

Function to filter values in PySpark

I'm trying to run a for loop in PySpark that needs a to filter a variable for an algorithm.
Here's an example of my dataframe df_prods:
+----------+--------------------+--------------------+
|ID | NAME | TYPE |
+----------+--------------------+--------------------+
| 7983 |SNEAKERS 01 | Sneakers|
| 7034 |SHIRT 13 | Shirt|
| 3360 |SHORTS 15 | Short|
I want to iterate over a list of ID's, get the match from the algorithm and then filter the product's type.
I created a function that gets the type:
def get_type(ID_PROD):
return [row[0] for row in df_prods.filter(df_prods.ID == ID_PROD).select("TYPE").collect()]
And wanted it to return:
print(get_type(7983))
Sneakers
But I find two issues:
1- it takes a long time to do that (longer than I got doing a similar thing on Python)
2- It returns an string array type: ['Sneakers'] and when I try to filter the products, this happens:
type = get_type(7983)
df_prods.filter(df_prods.type == type)
java.lang.RuntimeException: Unsupported literal type class java.util.ArrayList [Sneakers]
Does anyone know a better way to approach this on PySpark?
Thank you very much in advance. I'm having a very hard time learning PySpark.
A little adjustment on your function. This returns the actual string of the target column from the first record found after filtering.
from pyspark.sql.functions import col
def get_type(ID_PROD):
return df.filter(col("ID") == ID_PROD).select("TYPE").collect()[0]["TYPE"]
type = get_type(7983)
df_prods.filter(col("TYPE") == type) # works
I find using col("colname") to be much more readable.
About the performance issue you've mentioned, I really cannot say without more details (e.g. inspecting the data and the rest of your application). Try this syntax and tell me if the performance improves.

Divding column of Dataframe by constant value

I have a Data frame in below format.
| Occupation | wa_rating | Genre |
| engineer | 935 | Musical |
Now I want to divide Rating column of this Dataframe by totalRatings.
but when I am doing
resultDF = joinedDF.select(col("wa_rating")/totalRating)
It is giving me below error.
unsupported literal type class java.util.Arraylist
Likely your totalRating variable is a list. For example [100]. And you can't divide a number by a list. This throws your error:
resultDF = joinedDF.select(col("wa_rating")/[100])
but this does not
resultDF = joinedDF.select(col("wa_rating")/100)
Check that totalRating is an actual number (a float or integer). If it's a list containing a number, simply extract the number from it.
EDIT:
From your comments, we now know that your totalRating is a list. You can transform it to a number with:
totalRating = joinedDF3.groupBy().sum("Rating").collect()[0][0]

Oracle PL/SQL number to_char ignores 0's even when decimal part exist [duplicate]

This question already has an answer here:
Fetching value from a number column removes 0 before decimal
(1 answer)
Closed 5 months ago.
Im using the following code to convert a number(38,2) to the format "XXX.XXX.XXX,XX":
TO_CHAR(number, '999G999G999G999G999G999G999D99')
Although, when the number is 0 or 0.XX the 0 is eaten up :
Input: 0
Result: ",00"
Expected Result: "0,00"
Input: 0.23
Result: ",23"
Expected Result: "0,23"
Why does this happen, is there a way of solving this without manipulating the result string ?
Use 0 instead of 9
TO_CHAR(0.00, '999G999G999G999G999G999G990D99')
0 ensures that if there is no digit in that place it'll display 0.

How can I write crate-wide documentation?

In order to ensure that all public artifacts of my crate are documented (if minimally to start with), I specified #![deny(missing_docs)] in my lib.rs. This backfired.
I expected to write a documentation comment at the top and the code afterwards:
/// Hello world example for Rust.
#![deny(missing_docs)]
fn main() {
println!("Hello world!");
}
This fails with:
error: an inner attribute is not permitted following an outer doc comment
--> src/main.rs:3:3
|
3 | #![deny(missing_docs)]
| ^
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.
Reverting the order so that the attribute is first and the comment second:
#![deny(missing_docs)]
/// Hello world example for Rust.
fn main() {
println!("Hello world!");
}
Also fails:
error: missing documentation for crate
--> src/main.rs:1:1
|
1 | / #![deny(missing_docs)]
2 | |
3 | | /// Hello world example for Rust.
4 | |
5 | | fn main() {
6 | | println!("Hello world!");
7 | | }
| |_^
|
note: lint level defined here
--> src/main.rs:1:9
|
1 | #![deny(missing_docs)]
| ^^^^^^^^^^^^
I could not find how to actually write documentation for the crate itself. How should I be writing the crate's documentation to satisfy #![deny(missing_docs)]?
I found the hidden nugget in the book's Publishing a Crate to Crates.io section.
Regular documentation comments (starting with ///) document the next item, however a crate is nobody's next.
The solution is to switch to using another kind of comment, this time starting with //!, which documents the enclosing item.
And suddenly it works:
#![deny(missing_docs)]
//! Hello world example for Rust.
fn main() {
println!("Hello world!");
}