473,657 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

re quiz

True or False? (no fair looking it up)

(*) If repl is a string then re.sub(pattern, repl, s)
returns s with non-overlapping occurences of pattern
replaced by repl.

I assumed it was true - spent a few hours trying to
figure out what was going on with a certain re.sub,
then noticed that (*) is false:

(**) "If repl is a string, any backslash escapes in it are
processed. That is, "\n" is converted to a single newline
character, "\r" is converted to a linefeed, and so forth."

So I changed my r"\remark{Hint} " to r"\\remark{Hint }"
and things were fine.

A pointless question and then a practical one.

Pointless question: There must be a good reason for (**).
What would it be? Seems needlessly confusing to me (of
course a lot of things seem confusing to me...)

Maybe it's going to be confusing no matter what they do.
But "\\n" looks like it doesn't contain a newline, but it
gets converted to something that does.

(Another fascinating question is how they could phrase
the docs here so as to confuse nobody. Because "\n"
_is_ a newline, or so it's going to look to many people;
I'd spell it out:: "a string containing '\' followed by 'n' ".)

Practical question: What's a _complete_ list of the
escapes included in the "and so forth" in (**)?

(Or is there a function somewhere that will convert
r"\remark{Hint} " to r"\\remark{Hint }" for me, and
do the same for precisely the escpapes referred to
in the "and so forth"?)
David C. Ullrich
Jun 27 '08 #1
8 977
On Jun 12, 8:57 pm, David C. Ullrich <dullr...@spryn et.comwrote:
True or False? (no fair looking it up)

(*) If repl is a string then re.sub(pattern, repl, s)
returns s with non-overlapping occurences of pattern
replaced by repl.

I assumed it was true - spent a few hours trying to
figure out what was going on with a certain re.sub,
then noticed that (*) is false:
Well, the docs do say "Return the string obtained by replacing the
leftmost non-overlapping occurrences of pattern in string by the
replacement repl." -- care to tell us what "a certain re.sub" is, and
false in what way?
Jun 27 '08 #2
David C. Ullrich wrote:
(Or is there a function somewhere that will convert
r"\remark{Hint} " to r"\\remark{Hint }" for me, and
do the same for precisely the escpapes referred to
in the "and so forth"?)
I think you just have to escape the backslash:

re.sub(pattern, replacement_str ing.replace("\\ ", "\\\\"), s)

Anyway here's the list of troublemakers:
>>def means_trouble(c ):
.... try:
.... return re.sub("x", "\\" + c, "x") != "\\" + c
.... except:
.... return True
....
>>[c for c in map(chr, range(256)) if means_trouble(c )]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', 'a', 'b', 'f', 'g', 'n', 'r', 't', 'v']
Peter

Jun 27 '08 #3
David C. Ullrich <du******@spryn et.comwrote:
Practical question: What's a _complete_ list of the
escapes included in the "and so forth" in (**)?

(Or is there a function somewhere that will convert
r"\remark{Hint} " to r"\\remark{Hint }" for me, and
do the same for precisely the escpapes referred to
in the "and so forth"?)
As the documentation says:

Character escapes
Numbered Groups: \0 \1 \2 \3 ...
Named groups: \g<name>
Numbered groups with explicit termination of the number: \g<0\g<1...

But it doesn't matter what the complete list is. All of the escapes start
with \ so doubling all the \\ will prevent any of them being interpreted as
special so if you aren't wanting to substitute any groups into the string
just try repl.replace('\ \', r'\\')
--
Duncan Booth http://kupuguy.blogspot.com
Jun 27 '08 #4
On Thu, 12 Jun 2008 05:12:55 -0700 (PDT), John Machin
<sj******@lexic on.netwrote:
>On Jun 12, 8:57 pm, David C. Ullrich <dullr...@spryn et.comwrote:
>True or False? (no fair looking it up)

(*) If repl is a string then re.sub(pattern, repl, s)
returns s with non-overlapping occurences of pattern
replaced by repl.

I assumed it was true - spent a few hours trying to
figure out what was going on with a certain re.sub,
then noticed that (*) is false:

Well, the docs do say "Return the string obtained by replacing the
leftmost non-overlapping occurrences of pattern in string by the
replacement repl."
That's the _first sentence_, yes. I _quoted_ another sentence
(from an old version, istr it phrased slightly differently in
recent versions) in my post.
-- care to tell us what "a certain re.sub" is, and
false in what way?
Read the OP.
David C. Ullrich
Jun 27 '08 #5
On Thu, 12 Jun 2008 14:12:31 +0200, Peter Otten <__*******@web. de>
wrote:
>David C. Ullrich wrote:
>(Or is there a function somewhere that will convert
r"\remark{Hint }" to r"\\remark{Hint }" for me, and
do the same for precisely the escpapes referred to
in the "and so forth"?)

I think you just have to escape the backslash:

re.sub(pattern , replacement_str ing.replace("\\ ", "\\\\"), s)

Anyway here's the list of troublemakers:
Heh - I shoulda thought of that, determining which
ones cause trouble by checking to see which ones
cause trouble. Thanks.
>>>def means_trouble(c ):
... try:
... return re.sub("x", "\\" + c, "x") != "\\" + c
... except:
... return True
...
>>>[c for c in map(chr, range(256)) if means_trouble(c )]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', 'a', 'b', 'f', 'g', 'n', 'r', 't', 'v']
Peter
David C. Ullrich
Jun 27 '08 #6
On 12 Jun 2008 12:32:13 GMT, Duncan Booth
<du**********@i nvalid.invalidw rote:
>David C. Ullrich <du******@spryn et.comwrote:
>Practical question: What's a _complete_ list of the
escapes included in the "and so forth" in (**)?

(Or is there a function somewhere that will convert
r"\remark{Hint }" to r"\\remark{Hint }" for me, and
do the same for precisely the escpapes referred to
in the "and so forth"?)

As the documentation says:

Character escapes
Numbered Groups: \0 \1 \2 \3 ...
Named groups: \g<name>
Numbered groups with explicit termination of the number: \g<0\g<1...
Right - I was wondering about a complete list of character
escapes.
>But it doesn't matter what the complete list is. All of the escapes start
with \ so doubling all the \\ will prevent any of them being interpreted as
special so if you aren't wanting to substitute any groups into the string
just try repl.replace('\ \', r'\\')
Good point.

David C. Ullrich
Jun 27 '08 #7
David C. Ullrich schrieb:
>-- care to tell us what "a certain re.sub" is, and
false in what way?

Read the OP.
Well, aren't you funny. Maybe you should have referenced the other
thread so one can find the OP?

Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electron ics <47************ ***********@new s.freenet.de>
Jun 27 '08 #8
In article <qv************ @joeserver.home lan.net>,
Johannes Bauer <df***********@ gmx.dewrote:
David C. Ullrich schrieb:
-- care to tell us what "a certain re.sub" is, and
false in what way?
Read the OP.

Well, aren't you funny. Maybe you should have referenced the other
thread so one can find the OP?
What other thread? OP is sometimes Original Poster and
sometimes Original Post. In the original post in this
very thread I gave an quote from the docs and an example
illustrating the answer to the question I was asked.

Ok, I guess it's hard to find the top of the thread.
I wanted to replace a certain pattern with r"\remark{Hint} ".
I didn't understand why that didn't work until I read
the bit of the docs that I quoted in my original post:

(**) "If repl is a string, any backslash escapes in it are
processed. That is, "\n" is converted to a single newline
character, "\r" is converted to a linefeed, and so forth."
Regards,
Johannes
--
David C. Ullrich
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2637
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
5
5748
by: Vandana Rola | last post by:
Hello Everyone, I am a beginner in Javascript.I want to create fun quiz tool using javascript (no database). The feature of that tool is every question has five choices. The user needs to select three best choices. If they do, a message window pops up which says good choices or something and take them to next question. If they don't choose all of the best choices they get another message like try again but these are not the best choices...
4
7462
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of questions and answers to cycle through until the last question? Also, how can I give him the score after the last question. Thank you in advance. DAL. P.S. As a beginner, I figured I couldn't pass up the chance to learn something new, and to practice...
0
1646
by: philip | last post by:
hello, i am now developing a quiz application for my school using ASP.NET and SQL SERVER 2005, here is a senario: It will have 20 students for taking a quiz in a classroom, they have to answer randomly generated 100 questions in 90 minutes from database, each of those questions may have a picture (approximately 200K), and the picture is stored in the database as image type. i have 2 methods for retrieving 100 questions for each student.
2
3426
by: kenny | last post by:
I'm making a quiz to be posted on the internet. but I'm facing difficulties in finding a simple timer for the quiz (unlimited timing) which will keep on running regardless to the change of the page throughout the quiz. And well how to display the result of the quiz and te grade of the person who has taken the quiz....
0
4573
NoPeasHear
by: NoPeasHear | last post by:
I don't know what I am doing wrong... I used this tutorial... http://www.permadi.com/tutorial/flashMXQuiz/index.html It works with their quiz.xml file, but when I add an option for multiple correct answers, the results page always gives me a response of 0. What would I change from the tutorial to have multiple correct responses reflect in the results? <?xml version="1.0"?>
3
4398
by: Raqueeb Hassan | last post by:
Hello, I was helping one of my friend's school on setting up a online quiz system. They have the AMP systems to host php+mysql. The quiz script/software should have the following features: a. Excellent results screen (customizable) which can be printed right away. b. 3/5 simultaneously run quizes along with a authentication system. c. Should be able to handle MCQs (multiple choice questions) along with
1
3086
by: korr | last post by:
Hi there, i'm trying to develop a quiz in flash. Searching on the net, I found a quiz in flashkit from sephiroth.it by Alessandro Crugnola. His quiz has a script that puts the questions and the answers in random order. What i'm looking for is to not select the answers by clicking the mouse, but using the keyPress handler. Using the keyPress handler the script selects only the first answer I tried a lot but i didn't find any solution.. 2...
5
2172
nomad
by: nomad | last post by:
Hello Everyone: Just want to ask how easy would it be to build a quiz in Java. I have not use Java for a few months (5). Quiz would need the following: 1. T or F and mulitiple question, possible write the answer 2. Client must register and sign in to take the test/quiz (db is needed) 3. Quiz gives final results of the test: How many they got right, pass or fail 4. DB holds results of the tests. 5. Must be able to take the quiz on-line ie...
3
2212
by: empiresolutions | last post by:
I am building a app that creates quizzes. This is how it goes - - Create Quiz - Provide up to 10 different types of Quiz Results - Give up to 50 Questions - Each Question has up to 10 possible Answers. - Each Answer is assigned a Weighted value.... for each type of Quiz Result. - Weighted values are in the range of -6, 0, +6. - Each Quiz will also apply the same Weight range for if a M/F is taking and what of 6 age groups the taker is...
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.