How can I transform "email#domain.com" to "em***#domain.com"? - ruby

I want to transform emails only showing the first two characters and substitute the rest of them with '*' and keep the '#domain.com' as it is, how can I do it
I already have this code that works as expected but I would like to have a reduced way to do it, I don't know if there is a way to do it with gsub method and pass some ranges, any help will be appreciated
email = 'email#domain.com'
splitted_email = email.partition('#')
splitted_email.first[2...email.size-2]
splitted_email[0] = splitted_email.first[0...2] + ('*' *(splitted_email.first.size-2))
splitted_email.join
=> em***#domain.com

You may use a regex for this. A more or less readable one is
.gsub(/(?<=\A[^#]{2})[^#]*/) { |m| "*" * m.length }
What it does is matches
(?<=\A[^#]{2}) - a positive lookbehind that matches a location that is immediately preceded with:
\A - string start
[^#]{2} - two chars other than #
[^#]+ - any 1+ chars other than #.
The replacement is the block where we can manipulate the match: the * char is repeated the same number of times as the length of the match value.
See the Ruby demo:
email = 'email#domain.com'
p email.gsub(/(?<=\A[^#]{2})[^#]+/) { |m| "*" * m.length }
# => "em***#domain.com"

email.gsub(/(^.{2})[^#]*(#.*)$/,'\1***\2')
But i does not work with only one letter in front of #.

One possible solution using Object#then:
email.split('#').then { |a, b| a[0..-4] + '*' * 3 + '#' + b }
or
email.partition('#').then { |a, b, c| a[0..-4]+ '*' * 3 + b + c }
For the following cases it returns
email = 'email#domain.com' #=> "em***#domain.com"
email = 'emai#domain.com' #=> "e***#domain.com"
email = 'ema#domain.com' #=> "***#domain.com"
email = 'e#domain.com' #=> "***#domain.com"
But I don't know how you intend to manage when local-part is smaller than 4 chars.

Related

Can anyone give me advice on how to replace the number of letters with asterisks?

Here is my code and output below, i would like to have it so that instead of saying how many occurences, it would output the number of times the letter appears in asterisk form.
For exmaple if "a" appeared four times within a sentence the output would produce:
"a": ****
the_file='C:\Users\Jack\Documents\Ruby\Lab1\lyric.txt'
h = Hash.new
f = File.open(the_file, "r")
f.each_line { |line|
words = line.split(//)
words.each { |w|
if h.has_key?(w)
h[w] = h[w] + 1
else
h[w] = 1
end
}
}
# sort the hash by value, and then print it in this sorted order
h.sort{|a,b| a[1]<=>b[1]}.each { |elem|
puts "\"#{elem[0]}\" : #{elem[1]} occurrences"
}
Screenshot of my current program and output
Instead of #{elem[1]} occurences you just need to write #{'*' * elem[1]}
See method description for more details.
I would like to show another possible alternative way to achieve the word count.
Letting apart the file reading, let's consider the following string:
line = 'Here is my code and output below, i would like to have it so that instead of saying how many occurrences, it would output the number of times the letter appears in asterisk form.'
h = Hash.new(0)
line.downcase.each_char{ |ch| h[ch] += 1 if ('a'..'z').include? ch }
h.to_a.sort_by(&:last).reverse.each { |ch, count| puts "#{ch}: " + "*" * count}
Initialise the hash with default = 0 allow you to start the count without checking if key exists: Hash#default.
Iterate over the line by String#each_char
I counted only case insensitive letters, up to you
For sorting change the Hash into an Array with Hash#to_a
For printing the histogram, as shown in other posts
puts "\"#{elem[0]}\": " + '*' * elem[1]
+ for concatenate, string * number is to repeat certain string number times.
With strings in Ruby, you can use math operators against them. So, you know how many times the letter appears (in elem[1]). In that case you can just multiply the asterisks symbol by that amount:
"\"#{elem[0]}: #{'*' * elem[1]}\""

Ruby, looping through a string deleting groups of characters until a desired output is achieved

I have a coding problem I solved and want to refactor. I know there has to be a cleaner way of doing what I did.
The goal is to write a method that takes a string of "!" and "?" and reduces the string by eliminating all odd groupings of each symbol.
Example - a string "????!!!" would have an odd grouping of "!!!" because there are three in a row. These would be deleted from the string.
If there is only one "!" or "?" its left because it is not in a group.
Ex -
remove("!????!!!?") answer == "!"
# => ("!????!!!?" --> "!?????" --> "!")
In the first string, the only odd grouping is "!!!", once removed, it leaves a new string with an odd grouping "?????". You remove the next odd grouping so you're left with "!". This fits the desired output.
Another example
remove("!???!!") == ""
# => ("!???!!" --> "!!!" --> "")
Current code:
def remove(s)
arr = [s]
i = 0
until i == arr[0].length
s = s.chars.chunk{|c|c}.map{ |n,a| a.join }.select{|x| x if x.length.even? || x.length <= 1}.join
arr << s
i += 1
end
return arr[-1]
end
My code solves this problem and all test cases. I have a suspicion that my until loop can be removed/refactored so that I could solve this problem in one line and have spent hours trying to figure it out with no luck.
Suppose
str = "???!!!???!"
If we first remove the two groups "???" we are left with "!!!!", which cannot be reduced further.
If we first remove the group "!!!" we are left with "??????!", which cannot be reduced further.
If we are permitted to remove all odd groups of either character without reference to the effect that either has on the other, we obtain !, which cannot be reduced further.
It's not clear what rule is to be used. Here are three possibilities and code to implement each.
I will use the following two regular expressions, and in the first two cases a helper method.
Rq = /
(?<!\?) # do not match a question mark, negative lookbehind
\? # match a question mark
(\?{2})+ # match two question marks one or more times
(?!\?) # do not match a question mark, negative lookahead
/x # free-spacing regex definition mode
which is commonly written /(?<!\?)\?(\?{2})+(?!\?)/.
Similarly,
Rx = /(?<!!)!(!{2})+(?!!)/
def sequential(str, first_regex, second_regex)
s = str.dup
loop do
size = s.size
s = s.gsub(first_regex,'').gsub(second_regex,'')
return s if s.size == size
end
end
I apply each of the three methods below to two example strings:
str1 = "???!!!???!"
str2 = 50.times.map { ['?', '!'].sample }.join
#=> "?!!!?!!!?!??????!!!?!!??!!???!?!????!?!!!?!?!???!?"
Replace all odd groups of "?" then odd groups of "!" then repeat until no further removals are possible
def question_before_exclamation(str)
sequential(str, Rq, Rx)
end
question_before_exclamation str1 #=> "!!!!"
question_before_exclamation str2 #=> "??!??!?!!?!?!!?"
Replace all odd groups of "!" then odd groups of "?" then repeat until no further removals are possible
def exclamation_before_question(str)
sequential(str, Rx, Rq)
end
exclamation_before_question str1 #=> "??????!"
exclamation_before_question str2 #=> "??!????!!?!?!!?!?!!?"
Replace all odd groups of both "?" and "!" then repeat until no further removals are possible
Rqx = /#{Rq}|#{Rx}/
#=> /(?-mix:(?<!\?)\?(\?{2})+(?!\?))|(?-mix:(?<!!)!(!{2})+(?!!))/
def question_and_explanation(str)
s = str.dup
loop do
size = s.size
s = s.gsub(Rqx,'')
return s if s.size == size
end
end
question_and_explanation str1 #=> "!"
question_and_explanation str2 #=> "??!?!!?!?!!?!?!!?"
I don't know the exact Ruby syntax for this, but you could simplify your solution by using regular expressions:
Gather all matches of consecutive characters
if all matches are of even length or 1 exit
Test if matches are an odd length
if an odd length, replace with the empty string
else do nothing
Goto step 1
A solution in Perl would be:
#!perl
use strict;
use warnings;
use feature qw(say);
my $string = '!????!!!?';
sub reduce {
my ($s) = #_;
while ( my #matches = $s =~ m/((.)\2+)/g ) {
last if ! grep { length($_) > 1 && length($_) % 2 == 1 } #matches;
foreach my $match ( #matches ) {
$s =~ s/\Q$match// if length($match) > 1 && length($match) % 2 == 1;
}
}
return $s;
}
say reduce($string);
I could be wrong (this is ruby, after all) but I don't think you'll find a one-liner for this because ruby's utility functions generally aren't recursive. But you can use regex to simplify your logic, at the very least:
def remove(s)
while s =~ /(?<!\!)\!([\!]{2})+(?!\!)/ || s =~ /(?<!\?)\?([\?]{2})+(?!\?)/
s.gsub! /(?<!\!)\!([\!]{2})+(?!\!)/, "" # remove odd !
s.gsub! /(?<!\?)\?([\?]{2})+(?!\?)/, "" # remove odd ?
end
return s
end
To make the regex less mind-boggling, it helps to look at them with 'a' instead of '?' and '!':
/(?<!a)a([a]{2})+(?!a)/ #regex for 'a'
(?<!a) #negative lookbehind: the match cannot start with an 'a'
a([a]{2})+ #the match should be an 'a' followed by 1 or more pairs
(?!a) #negative lookahead: the match cannot end with an 'a'
It should be simple enough with a regular expression replacement
def remove(string)
begin
original = string
string.gsub!(/(\!{3,})|(\?{3,})/) { |s| s.length.even? ? s : '' }
end until original == string
string
end
puts remove("!????!!!?").inspect # answer == "!"
puts remove("!???!!").inspect # answer == ""
puts remove("!????!!").inspect # answer == "!????!!"

Replacing hyphens in words with the next letter capitalized

I have a symbol like the following. Whenever the symbol contains the "-" hyphen mark, I want to remove it and upcase the subsequent letter.
I am able to do it like so:
sym = :'new-york'
str = sym.to_s.capitalize
/-(.)/.match(str)
str = str.gsub(/-(.)/,$1.capitalize)
=> "NewYork"
This required four lines. Is there a more elegant way to create CamelCase (upper CamelCase e.g. NewYork, NewJersey, BucksCounty) from hyphened words in Ruby?
Here's one way:
sym.to_s.split('-').map(&:capitalize).join #=> "NewYork"
sym.to_s.gsub(/(-|\A)./) { $&[-1].upcase }
or
sym.to_s.gsub(/(-|\A)./) { |m| m[-1].upcase }
r = /
([[:alpha:]]+) # match 1 or more letters in capture group 1
- # match a hyphen
([[:alpha:]]+) # match 1 or more letters in capture group 2
/x # free-spacing regex definition mode
sym = :'new-york'
sym.to_s.sub(r) { $1.capitalize + $2.capitalize }
#=> "NewYork"

Ruby regex checks string for variations of pattern of same length

I was wondering how you construct the regular expression to check if the string has a variation of a pattern with the same length. Say the string is "door boor robo omanyte" how do I return the words that have the variation of [door]?
You can easily get all the possible words using Array#permutation. Then you can scan for them in provided string. Here:
possible_words = %w[d o o r].permutation.map &:join
# => ["door", "doro", "door", "doro", "droo", "droo", "odor", "odro", "oodr", "oord", "ordo", "orod", "odor", "odro", "oodr", "oord", "ordo", "orod", "rdoo", "rdoo", "rodo", "rood", "rodo", "rood"]
string = "door boor robo omanyte"
string.scan(possible_words.join("|"))
# => ["door"]
string = "door close rood example ordo"
string.scan(possible_words.join("|"))
# => ["door", "rood", "ordo"]
UPDATE
You can improve scan further by looking for word boundary. Here:
string = "doorrood example ordo"
string.scan(/"\b#{possible_words.join('\b|\b')}\b"/)
# => ["ordo"]
NOTE
As Cary correctly pointed out in comments below, this process is quite inefficient if you intend to find permutation for a fairly large string. However it should work fine for OP's example.
If the comment I left on your question correctly interprets the question, you could do this:
str = "door sit its odor to"
str.split
.group_by { |w| w.chars.sort.join }
.values
.select { |a| a.size > 1 }
#=> [["door", "odor"], ["sit", "its"]]
This assumes all the letters are the same case.
If case is not important, just make a small change:
str = "dooR sIt itS Odor to"
str.split
.group_by { |w| w.downcase.chars.sort.join }
.values
.select { |a| a.size > 1 }
#=> [["dooR", "Odor"], ["sIt", "itS"]]
In my opinion the fastest way to find this will be
word_a.chars.sort == word_b.chars.sort
since we are using the same characters inside the words
IMO, some kind of iteration is definitely necessary to build a regular expression to match this one. Not using a regular expression is better too.
def variations_of_substr(str, sub)
# Creates regexp to match words with same length, and
# with same characters of str.
patt = "\\b" + ( [ "[#{sub}]{1}" ] * sub.size ).join + "\\b"
# Above alone won't be enough, characters in both words should
# match exactly.
str.scan( Regexp.new(patt) ).select do |m|
m.chars.sort == sub.chars.sort
end
end
variations_of_substr("door boor robo omanyte", "door")
# => ["door"]

Ruby string concatenation add space

I need to make a method that repeats a given word but I think I am designing it wrong. I need spaces between the words, what am I missing here?
def repeat(word, repeats=2)
sentence = word.to_s * repeats
return sentence
end
Of course, you are missing spaces.
You could have done it like this:
def repeat(word, repeats = 2)
Array.new(repeats, word).join(" ")
end
You can write the code as below :
def repeat(word, repeats=2)
([word] * repeats).join(" ")
end
repeat("Hello",4)
# => "Hello Hello Hello Hello"
Here's one closer to your approach and without using a temporary array:
def repeat(word, repeats=2)
("#{word} " * repeats).chop
end
"#{word} " converts word to a string using string interpolation and appends a space
… * repeats creates a string containing repeats copies
.chop returns the string with the last character removed (the trailing space)
Not having to create an array makes the code a bit faster.
w = 'kokot'
n = 13
n.times.map { w.each_char.to_a.shuffle.join }.tap { |a, _| a.capitalize! }.join(' ') << ?.

Resources