Skip to content

Conversation

hulufei
Copy link
Contributor

@hulufei hulufei commented Mar 13, 2020

For example: leetcode problem 202

@hulufei hulufei changed the title Support display sup/sub styles of numbers Support display sup/sub style for numbers Mar 13, 2020
@clearloop
Copy link
Owner

clearloop commented Mar 13, 2020

Thanks!!!

 𝝺 pcs cargo run p 202 Finished dev [unoptimized + debuginfo] target(s) in 0.13s Running `target/debug/leetcode p 202` [202] Happy Number is on the run... Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers. -------------------------------------------------- Example:  Input: 19 Output: true Explanation: 1² + 9² = 82 8² + 2² = 68 6² + 8² = 100 1² + 0² + 0² = 1 
@clearloop clearloop merged commit 5ae6e61 into clearloop:master Mar 13, 2020
Comment on lines +112 to +138
pub fn superscript(n: u8) -> String {
if n == 0 {
"⁰".to_owned()
} else if n == 1 {
"¹".to_owned()
} else if n == 2 {
"²".to_owned()
} else if n == 3 {
"³".to_owned()
} else if n == 4 {
"⁴".to_owned()
} else if n == 5 {
"⁵".to_owned()
} else if n == 6 {
"⁶".to_owned()
} else if n == 7 {
"⁷".to_owned()
} else if n == 8 {
"⁸".to_owned()
} else if n == 9 {
"⁹".to_owned()
} else if n >= 10 {
superscript(n / 10) + &superscript(n % 10)
} else {
n.to_string()
}
}
Copy link
Owner

@clearloop clearloop Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can write like:

pub fn superscript(n: u8) -> String { match n { 0 => "⁰".to_string(), 1 => "¹".to_string(), 2 => "²".to_string(), 3 => "³".to_string(), 4 => "⁴".to_string(), 5 => "⁵".to_string(), 6 => "⁶".to_string(), 7 => "⁷".to_string(), 8 => "⁸".to_string(), 9 => "⁹".to_string(), x if x > 10 => (superscript(n / 10).parse().unwrap_or(0) + &superscript(n % 10).parse().unwrap_or(0)) .to_string(), _ => n.to_string(), } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants