[LCWO LOGO]  

Login

User name:
Password:


Language
Български Português brasileiro
Bosanski Català
繁體中文 Česky
Dansk Deutsch
English Español
Suomi Français
Ελληνικά Hrvatski
Magyar Italiano
日本語 한국어
Bahasa Melayu Nederlands
Norsk Polski
Português Română
Русский සිංහල
Slovenščina Srpski
Svenska ภาษาไทย
Türkçe Українська
简体中文
Who is online? (27)


LCWO Discussion Forum [Atom LCWO Forum Feed]

This is a simple discussion forum for LCWO users. Feel free to use it for any kind of discussion related to this website.

Thread: Detect Spaces in Code Groups

Back to the Forum

AuthorText


Posted: 2023-01-24 11:53
This is just an idea in python that works similarly to the Code Groups "Check Result" function, but can detect spaces.

****

import difflib

TERMINAL_COLOR_RED='�33[0;31m'
TERMINAL_COLOR_GREEN='�33[0;32m'
TERMINAL_COLOR_BLUE='�33[0;34m'
TERMINAL_COLOR_NONE='�33[0m'

def diff_strings(a, b):
a = a.rstrip()
b = b.rstrip()
output = []
errs = 0
matcher = difflib.SequenceMatcher(None, a, b)
for opcode, a0, a1, b0, b1 in matcher.get_opcodes():
if opcode == "equal":
output.append(TERMINAL_COLOR_GREEN)
output.append(a[a0:a1])
elif opcode == "insert":
output.append(TERMINAL_COLOR_BLUE)
output.append(b[b0:b1])
errs += b1-b0
elif opcode == "delete":
output.append(TERMINAL_COLOR_RED)
append_string = a[a0:a1]
append_string = append_string.replace(' ','_')
output.append(append_string)
errs += a1-a0
elif opcode == "replace":
output.append(TERMINAL_COLOR_NONE)
output.append(a[a0:a1])
errs += a1-a0
print( "".join(output))
return errs

random_str = "?AGO HS0Y5, SZY0IDV 61G2YS7 KY, 0DZ/KF ,,UMP ,AR VB6KHW 2Q3/=0 Z5,YA I//5"
user_str = ",AGO 550Y5, SZY0IDV 61G27KY? 0DZ/KF ,,UMP,RAVB6KHW 2Q3/=0 Z5,YAI//5"

#result
# ?AGO HS0Y5, SZY0IDV 61G2YS7_KY, 0DZ/KF ,,UMP_,RAR_VB6KHW 2Q3/=0 Z5,YA_I//5

total_errs = diff_strings(random_str,user_str)
print("total errors =",total_errs)

Back to the Forum

You must be logged in to post a message.