I am trying to create password Generate in ruby. At the moment all is working just got stuck at the final piece of generating the password.
I asked user if he/she would like the password to include numbers, lowercase or uppercase.
If YES, user will enter 1 and 0 for NO.
I used the code below to generate password if everything is 1. Meaning user want to include numbers, lowercase and uppercase.
if numbers == 1 && lowercase == 1 && uppercase == 1
passGen = [(0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a].flatten.sample(10)
end
p passGen
This works 90% of the time. 10% of the time the generated password will not include say any numbers. But everything else present. I am not sure if this is because of the size or length of Array from which the password is sampled.
Anyway lets go to the main problem below
Here is the problem, I am struggling to write the code to generate password if one or more of input is 0. That's if user don't want to include numbers. Or no numbers and uppercase etc . As I can't predict what user may want or not want. I need help on this please.
Thank you.
You will need to make your input array more dynamic:
passGen = []
passGen += (0..9).to_a if numbers == 1
passGen += ('A'..'Z').to_a if uppercase == 1
passGen += ('a'..'z').to_a if lowercase == 1
passGen.sample(10).join
Now, to tackle your other issue with missing characters - this is caused as you are simply taking 10 random characters from an array. So it can just take, for example, all digits.
To tackle this you need to get one character from each generator first and then generate the remaining characters randomly and shuffle the result:
def generators(numbers:, lowercase:, uppercase:)
[
(0..9 if numbers),
('A'..'Z' if uppercase),
('a'..'z' if lowercase)
].compact.map(&:to_a)
end
def generate_password(generators:, length:, min_per_generator: 1)
chars = generators.flat_map {|g| Array.new(min_per_generator) { g.sample }}
chars += Array.new(length - chars.length) { generators.sample.sample }
chars.shuffle.join
end
gens = generators(numbers: numbers == 1, uppercase == 1, lowercase: lowercase == 1)
Array.new(10) { generate_password(generators: gens, length: 10) }
The code doesn't know it needs to include a digit/letter from every group. The sample takes random signs and since you a basically sampling 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz there is a possibility that all the signs will not be digits.
The easiest way to fix it is to check if a sign from every group is in the "password" and then replace a random sign with a sign from group that is not present.
If I were to program this I would do it like that
def random_from_range(range)
range.to_a.sample.to_s
end
def passGen(numbers, lowercase, uppercase)
result = ''
possibleSigns = []
if numbers == 1
range = (0..9)
result += random_from_range(range)
possibleSigns += range.to_a
end
if lowercase == 1
range = ('A'..'Z')
result += random_from_range(range)
possibleSigns += range.to_a
end
if uppercase == 1
range = ('a'..'z')
result += random_from_range(range)
possibleSigns += range.to_a
end
desired_lenth = 10
while result.length < desired_lenth
result += possibleSigns.sample.to_s
end
result
end
puts passGen(1,1,1)
By saying (0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a, you're creating an Array of 10 + 26 + 26 = 62 elements, and then you pick only 10 elements out of it.
At your place I'd wrap password generation around an until block:
def generate_password_with_digits_and_caps
[(0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a].flatten.sample(10).join
end
passGen = ''
until passGen.match(/[A-Z]/) && passGen.match(/[a-z]/) && passGen.match(/\d/)
passGen = generate_password_with_digits_and_caps
end
This could also work (closer to your snipppet):
if numbers == 1 && lowercase == 1 && uppercase == 1
passGen = ''
until passGen.match(/[A-Z]/) && passGen.match(/[a-z]/) && passGen.match(/\d/)
passGen = [(0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a].flatten.sample(10).join
end
end
Start with something simple and stupid:
passGen = (('0'..'9').to_a.sample(1)+ ('A'..'Z').to_a.sample(1)+('a'..'z').to_a.sample(8).shuffle).join
Technically speaking, this already fulfills your requirement. From the viewpoint of aesthetics and security, the disadvantage here is that the number of upper case characters is always 8. A more elegant solution would be to find three non-zero integers which add up to 10, and can be used as the arguments for the sample call. Also, if no numbers are requested, you simply pass 0 as argument to sample.
Since this exceeds the scope of your question, and I don't even know whether you want to go so far, I don't elaborate on this here further.
Related
I'm trying to split a word with a '.' after every letter which I was successful in doing, however, my next step is to split the current splitted words again but I dont want to repeat variations.
my expected output is this:
input word: amaxa
first loop will give - a.maxa, am.axa, ama.xa, amax.a
then the next split should give - a.m.axa, a.ma.xa,a.max.a,
Essentially I wanted different variations of the word with '.' being added when a full loop had been exhausted however, my main issue was I had '.'s appearing next to each other and I tried to use a continue statement but it didn't work. Below is my source code
print("enter email without #gmail.com")
word = input("word: ")
lenofword = len(word) - 1
for i in range(0,lenofword):
sliceword = word[:1+i] + "." + word[1+i:]
lis.append(sliceword)
print(sliceword)
for j in range(0,lenofword):
slices = sliceword[:1+j] + "." + sliceword[j+1:]
if slices[i:] == slices[:]:
continue
print(slices)
ouput given:
enter email without #gmail.com
word: amax
a.max
am.ax
a.m.ax
am..ax
am..ax
ama.x
a.ma.x
am.a.x
ama..x
basically i want to get rid of the 'am..ax' and 'ama..x'
It is easier to choose the two locations where the dots should be placed (may be the same location for the single dot case). For example:
for i in range(1, lenofword + 1):
for j in range(i, lenofword + 1):
sliced = ".".join(filter(None, [word[:i], word[i:j], word[j:]]))
print(sliced)
This will print the following for the input word "amax":
a.max
a.m.ax
a.ma.x
am.ax
am.a.x
ama.x
I need to take a string and assign each character to a new string variable for a Text To Speech engine to read out each character separately, mainly to control the speed at which it's read out by adding pauses in between each character.
The string contains a number which can vary in length from 6 digits to 16 digits, and I've put the below code together for 6 digits but would like something neater to handle any different character count.
I've done a fair bit of research but can't seem to find a solution, plus I'm new to Groovy / programming.
OrigNum= "12 34 56"
Num = OrigNum.replace(' ','')
sNum = Num.split("(?!^)")
sDigit1 = sNum[0]
sDigit2 = sNum[1]
sDigit3 = sNum[2]
sDigit4 = sNum[3]
sDigit5 = sNum[4]
sDigit6 = sNum[5]
Edit: The reason for needing a new variable for each character is the app that I'm using doesn't let the TTS engine run any code. I have to specifically declare a variable beforehand for it to be read out
Sample TTS input: "The number is [var:sDigit1] [pause] [var:sDigit2] [pause]..."
I've tried using [var:sNum[0]] [var:sNum[1]] to read from the map instead but it is not recognised.
Read this about dynamically creating variable names.
You could use a map in your stuation, which is cleaner and more groovy:
Map digits = [:]
OrigNum.replaceAll("\\s","").eachWithIndex { digit, index ->
digits[index] = digit
}
println digits[0] //first element == 1
println digits[-1] //last element == 6
println digits.size() // 6
Not 100% sure what you need, but to convert your input String to output you could use:
String origNum = "12 34 56"
String out = 'The number is ' + origNum.replaceAll( /\s/, '' ).collect{ "[var:$it]" }.join( ' [pause] ' )
gives:
The number is [var:1] [pause] [var:2] [pause] [var:3] [pause] [var:4] [pause] [var:5] [pause] [var:6]
In my Ruby class, I'm building a Connect Four game that my professor will run in command prompt.
It has to be done with a double array and while loops, and no break/exit/abort, loop do, classes, instance, or global variables.
My grid array is made up of 64 '.' placeholders in a nested array. I am trying to start at the bottom row of my 8x8 grid, and drop in a player's piece: 'X' or '0'.
I'm not sure how to move upward from row 8/index 7 to row 7/index 6 if the bottom row of a column has already been taken. Do I use if or case statements? Do I decrement the row? I have tried putting in if/elsif, but I got nowhere.
def print_playing_grid (playing_board)
puts "1 2 3 4 5 6 7 8"
playing_board.each do |row|
puts row.join(" ")
end
end
print_playing_grid(grid_array)
# this 'win'/while is only here for testing so the board will repeat on screen
win = false
while win == false
puts
puts "Please select a column to make your move (Player X):"
user_choice = gets.to_i
row = 7
column = user_choice - 1
while row < grid_array.size
grid_array[row][column] = 'X'
row += 1
end
puts
print_playing_grid(grid_array)
end
As it's only two conditions (cell is occupied or not) I'd be more likely to use an if rather than a case... I would use case for multiple tests as it can be a little cleaner than if and elsif.
player_token = 'X'
row = grid_array.size - 1
column = user_choice - 1
while (grid_array.size - row) <= grid_array.size
if grid_array[row][column] == '.'
grid_array[row][column] = player_token
row = -1 # (to exit the while loop)
else
row -= 1
end
end
for an example of where I'd consider using a case...
player_token = 'X'
row = grid_array.size - 1
column = user_choice - 1
dropping_a_token = true
while dropping_a_token
case
when row < 0
dropping_a_token = false
when grid_array[row][column] == '.'
grid_array[row][column] = player_token
dropping_a_token = false
else
row -= 1
end
end
import sgenrand
# program greeting
print("The purpose of this exercise is to enter a number of coin values")
print("that add up to a displayed target value.\n")
print("Enter coins values as 1-penny, 5-nickel, 10-dime,and 25-quarter.")
print("Hit return after the last entered coin value.")
print("--------------------")
#print("Enter coins that add up to 81 cents, one per line.")
total = 0
#prompt the user to start entering coin values that add up to 81
while True:
final_coin= sgenrand.randint(1,99)
print ("Enter coins that add up to", final_coin, "cents, on per line")
user_input = int(input("Enter first coin: "))
if user_input != 1 and user_input!=5 and user_input!=10 and user_input!=25:
print("invalid input")
total = total + user_input
while total != final_coin:
user_input = int(input("Enter next coin:"))
total = total + user_input
if user_input == input(" "):
break
if total > final_coin:
print("Sorry - total amount exceeds", (final_coin))
if total < final_coin:
print("Sorry - you only entered",(total))
if total== final_coin:
print("correct")
goagain= input("Try again (y/n)?:")
if goagain == "y":
if goagain == "n":
print("Thanks for playing ... goodbye!" )
I've been trying to create this loop, so it can repeat the whole program at the end when the user accepts/ if he accepts to do it again at the end.
I know you have to have a while statement around your whole program, but with my while true statement at the top, it only repeats the first part of my program and not the whole thing.
Well one thing you should be careful of is that you do not set the
total = 0
at the start of each loop. So when the user plays the game again. He will continue using his previous total. You should move total = 0 to the start of the loop
while True:
total = 0
Additionally, you need to deindent you first
while True
statement as it does not align properly with the rest of your code.
Finally, you need to allow the user to exit the while loop after he selects No for trying again.
if goagain == "n":
print("Thanks for playing ... goodbye!" )
break
This can be done by applying a break statement.
Its not a proper code but I want to know if there is a way to search just one word w./o using .split() as it forms a list and i dont want that with this snippet:
f=(i for i in fin.xreadlines())
for i in f:
try:
match=re.search(r"([A-Z]+\b) | ([A-Z\'w]+\b) | (\b[A-Z]+\b) | (\b[A-Z\'w]+\b) | (.\w+\b)", i) # | r"[A-Z\'w]+\b" | r"\b[A-Z]+\b" | r"\b[A-Z\'w]+\b" | r".\w+\b"
Also can i make a reusable class module like this
class LineReader: #Intended only to be used with for loop
def __init__(self,filename):
self.fin=open(filename,'r')
def __getitem__(self,index):
line=self.fin.xreadline()
return line.split()
where say f=LineReader(filepath)
and for i in f.getitem(index=line number 25) loop starts from there?
i dont know how to do that.any tips?
To get the first word of a line:
line[:max(line.find(' '), 0) or None]
line.find(' ') searches for the first whitespace, and returns it. If there is no whitespace found it returns -1
max( ... ), 0) makes sure the result is always greater than 0, and makes -1 0. This is usefull because bool(-1) is True and bool(0) is False.
x or None evaluates to x if x != 0 else None
and finaly line[:None] is equal to line[:], which returns a string identical to line
First sample:
with open('file') as f:
for line in f:
word = line[:max(line.find(' '), 0) or None]
if condition(word):
do_something(word)
And the class (implemented as a generator here)
def words(stream):
for line in stream:
yield line[:max(line.find(' '), 0) or None]
Which you could use like
gen = words(f)
for word in gen:
if condition(word):
print word
Or
gen = words(f)
while 1:
try:
word = gen.next()
if condition(word):
print word
except StopIteration:
break # we reached the end
But you also wanted to start reading from a certain linenumber. This can't be done very efficient if you don't know the lengths of the lines. The only way is reading lines and discarding them until you reach the right linenumber.
def words(stream, start=-1): # you could replace the -1 with 0 and remove the +1
for i in range(start+1): # it depend on whether you start counting with 0 or 1
try:
stream.next()
except StopIteration:
break
for line in stream:
yield line[:max(line.find(' '), 0) or None]
Be aware that you could get strange results if a line would start with a space. To prevent that, you could insert line = line.rstrip() at the beginning of the loop.
Disclaimer: None of this code is tested