Jump to content

How to add random hyphens between letters?


youtuber
 Share

Recommended Posts

For example, to add random hyphens like this

w-ord

wor-d

wo-rd

d-row

dro-w

dr-ow

o-wrd...

I tried a lot but I couldn't solve the logic

Local $aWord[1] = ['word']
Local $aRandomAdd = ""
For $i = 0 To Ubound($aWord)-1
   $aRandomAdd = $aWord[$i]
   $aRandomAdd &= Random(1,StringRight($aWord[$i], $i) & "-",1)
Next
ConsoleWrite($aRandomAdd & @CRLF)

 

Link to comment
Share on other sites

?

#include <array.au3>
Local $sWord = 'word'
Local $iNrWantedRandoms = 5, $iRandom, $sStrLen = StringLen($sWord)
SRandom(@MSEC)

ConsoleWrite('randomly place an hyphen' & @CRLF)
For $i = 0 To $iNrWantedRandoms
    $iRandom = Random(1, $sStrLen - 1, 1)
    ConsoleWrite(StringLeft($sWord, $iRandom) & '-' & StringRight($sWord, $sStrLen - $iRandom) & @CRLF)
Next

ConsoleWrite("randomly place an hyphen and also shuffle word's letters" & @CRLF)
Local $aWord = StringToASCIIArray($sWord)
For $i = 0 To $iNrWantedRandoms
    _ArrayShuffle($aWord)
    $sRandomWord = StringFromASCIIArray($aWord)
    $iRandom = Random(1, $sStrLen - 1, 1)
    ConsoleWrite(StringLeft($sRandomWord, $iRandom) & '-' & StringRight($sRandomWord, $sStrLen - $iRandom) & @CRLF)
Next

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

#include<string.au3>

$str = "word"
$insrt = "-"
$sOut = ""

for $i = 0 to stringlen($str) * 2 - 1
   $sOut &= $i < stringlen($str) ? _StringInsert($str , $insrt , random(1 , stringlen($str))) & @LF : stringreverse(_StringInsert($str , $insrt , random(1 , stringlen($str)))) & @LF
next

msgbox(0, '' , $sOut)

 

did you want them to be in unique positions.  more shuffled than random?  or really random?  and did you want the reverse strings too? 

edit:  i also assumed you didnt want the potential of a leading or trailing hyphen...

edit: also, my bad i see now they are shuffled, not just reversed.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

nothing related to @youtuber request(randomness) but still....

#include <Array.au3>
#include<string.au3>

$a_String = 'word'
$a_Array = StringSplit($a_String & '-', "", $STR_NOCOUNT)
$a_suffel = _ArrayPermute($a_Array, '')
_ArrayDelete($a_suffel, 0)
_ArrayDisplay($a_suffel)
;$b_string = _ArrayToString($a_suffel)
;Msgbox(0,"", StringRegExpReplace($b_string , "\|(.*?)" , "" & @CRLF))

& not sure use of StringRegExpReplace is correct.

Edited by jugador
Link to comment
Share on other sites

Quote

but why are sometimes similar words listed

I think because the goal of 'random' was a misnomer.  Hyphens could all be in the same position and that position still have been randomly selected each time.

It is seeming more that you want:

1) the string shuffled

2) a random hyphen placed within the string (not before or after)

3) repeat steps 1 and 2 ensuring the results are all unique

is that accurate?  do you want the string shuffled every time around the loop?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

For a complete list of all allowed results:

Local $s = "autoit"

Local $a = _ArrayPermute(StringSplit($s & '-', "", 3))
Local $b = StringSplit(StringTrimRight(StringRegExpReplace(_ArrayToString($a, ',', 1) & ',', "((?<!\w|^)-\w+,|\w+-,)", ""), 1), ',', 3)
$b = _ArrayUnique($b, 0, 0, 0, 0)
_ArrayDisplay($b)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd Thanks,this wonderful :)

 

Edited

#include <Array.au3>
Local $s = "autoit"

$a_Array = StringSplit($s & '-', "", $STR_NOCOUNT)
$a = _ArrayPermute($a_Array, '')
Local $b = StringSplit(StringTrimRight(StringRegExpReplace(_ArrayToString($a, ',', 1) & ',', "((?<!\w|^)-\w+,|\w+-,)", ""), 1), ',', 3)
$b = _ArrayUnique($b, 0, 0, 0, 0)
;~ _ArrayDisplay($b)
For $i = 0 To UBound($b) - 1
    ConsoleWrite($b[$i] & @CRLF)
Next

 

Edited by youtuber
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...