~horvath-arpad-roik/+junk/ec

« back to all changes in this revision

Viewing changes to bin/interval.py

  • Committer: Arpad Horvath
  • Date: 2013-10-08 20:47:47 UTC
  • Revision ID: horvath.arpad.szfvar@gmail.com-20131008204747-olw476fu6p3cx68m
bin/*py python3 compatibility and some other improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
  See: www.roik.bmf.hu/~ahorvath/feladatsor
13
13
   (It's only in Hungarian language this time.) """
 
14
from __future__ import print_function
14
15
 
15
16
 
16
17
old_docstring=r"""
47
48
## import part
48
49
##################################################
49
50
import os
50
 
import string
51
51
from random import randint, choice
52
52
import re
53
53
#import re
55
55
from lang import lang
56
56
 
57
57
try:
58
 
  exec 'from setup_%s import decimal_point, times' % lang
 
58
  exec('from setup_%s import decimal_point, times' % lang)
59
59
except ImportError:
60
 
  print "I use english instead of %s." % lang
 
60
  print("I use english instead of %s." % lang)
61
61
  from setup_en import decimal_point, times
62
62
 
63
63
 
64
64
try:
65
 
  exec 'from lang_%s import env, mesg, err' % lang
 
65
  exec('from lang_%s import env, mesg, err' % lang)
66
66
except ImportError:
67
 
  print "I use english instead of %s." % lang
 
67
  print("I use english instead of %s." % lang)
68
68
  from lang_en import env, mesg, err
69
69
 
70
70
verbose = 0
74
74
## elif lang in ['en', 'us']:
75
75
##   times = r"\times"
76
76
## else:
77
 
##   print "There is no support for language %s according to multiplication sign." % lang
 
77
##   print("There is no support for language %s according to multiplication sign." % lang)
78
78
##   times = r"\times"
79
79
 
80
80
###############################################
102
102
         self.format = 'exponential'    (It is 'standard', if there is not an 'e' in num_string.)
103
103
         self.dec_point = ','  # In Hungary the decimal 'point' is ',', not '.'
104
104
    """
105
 
    num_string = string.strip(num_string)  # ' -03.23e015  ' --> '-03.23e015'
 
105
    num_string = num_string.strip()  # ' -03.23e015  ' --> '-03.23e015'
106
106
 
107
107
    if ',' in  num_string:
108
108
      dec_point = ','
109
 
      num_string = string.replace(num_string, ',', '.')
 
109
      num_string = num_string.replace(',', '.')
110
110
    if '.' in  num_string:
111
111
      dec_point = '.'
112
112
    else:
128
128
 
129
129
    # One character number
130
130
    if len(num_string) == 1:
131
 
      self.factor = sign*string.atoi(num_string)
 
131
      self.factor = sign*int(num_string)
132
132
      self.exponent = 0
133
133
      return
134
134
 
135
135
    #  Handling of 'e'
136
 
    string.lower(num_string)  # 'E' --> 'e' if it is any
137
 
    parts = string.split(num_string, 'e')  # ('3.23', '15')
 
136
    num_string.lower()  # 'E' --> 'e' if it is any
 
137
    parts = num_string.split('e')  # ('3.23', '15')
138
138
    if len(parts) == 1:
139
139
      self.format = 'standard'
140
140
      self.exponent = '0'  # Exponent is 0.
143
143
      self.factor, self.exponent = parts
144
144
      self.format = 'exponential'
145
145
    else:
146
 
      raise ValueError, "more then one 'e' is in the number_string"
147
 
    self.exponent = string.atoi(self.exponent)
 
146
      raise ValueError("more then one 'e' is in the number_string")
 
147
    self.exponent = int(self.exponent)
148
148
 
149
149
    # Handling of dec_point '.'
150
 
    factor_parts =  string.split(self.factor, '.')
 
150
    factor_parts =  self.factor.split('.')
151
151
    if len(factor_parts) == 2: # There is a point in it.
152
152
      integer, fract = factor_parts
153
153
      self.exponent = self.exponent - len(fract)
171
171
##     factor = factor + '0'
172
172
##     exponent = exponent - 1
173
173
 
174
 
    self.factor = sign * string.atoi(self.factor)      # (-323, 13)
 
174
    self.factor = sign * int(self.factor)      # (-323, 13)
175
175
 
176
176
  def __str__(self):
177
 
    try: string = "%de%d (%s)" % (self.factor, self.exponent, self.format)
178
 
    except TypeError: string = 'Not well defined.'
 
177
    try:
 
178
        string = "%de%d (%s)" % (self.factor, self.exponent, self.format)
 
179
    except TypeError:
 
180
        string = 'Not well defined.'
179
181
    return string
180
182
 
181
183
 
183
185
  a = Exponential()
184
186
  for num_string in ['200','10','1000','-2e24']:
185
187
    a.set(num_string)
186
 
    print num_string, a
187
 
    print 'Factor: %d, exponent: %d, format: %s' % (a.factor, a.exponent, a.format)
 
188
    print(num_string, a)
 
189
    print('Factor: %d, exponent: %d, format: %s' % (a.factor, a.exponent, a.format))
188
190
 
189
191
 
190
192
## It was the original plan
205
207
##        '1' --> (1, 0)
206
208
 
207
209
##    The 4. point is only a plan.
208
 
## ##    4. If there is no stronger condition, the output has 2 (valuable??) digits.
209
 
## ##                                           #hu 2 értékes jegy
 
210
## ##    4. If there is no stronger condition, the output has 2 significant digits.
210
211
## ##   """
211
212
 
212
213
 
231
232
      exp.factor = exp.factor * (10**diff)
232
233
 
233
234
  if exp1.factor > exp2.factor:
234
 
    raise ValueError, 'the first number in the interval is the bigger'
 
235
    raise ValueError('the first number in the interval is the bigger')
235
236
 
236
237
  # format
237
238
  if exp1.format == exp2.format == 'standard':
248
249
  if exp1.dec_point == exp2.dec_point:
249
250
    dec_point = exp1.dec_point
250
251
  else:
251
 
    raise ValueError, 'there are two types of decimal point'
 
252
    raise ValueError('there are two types of decimal point')
252
253
 
253
254
  return [exp1.factor, exp2.factor], common_exponent, dec_point, format
254
255
 
255
256
def same_exponent_test():
256
257
  #same_exponent = same_exponent2
257
 
  print "same_exponent( '-2e23','3e24' )    -->",
258
 
  print same_exponent( '-2e23','3e24' )
259
 
 
260
 
  print "same_exponent( '1000','20000' )    -->",
261
 
  print same_exponent( '1000','20000' )
262
 
 
263
 
  print "same_exponent( '-2','5' )    -->",
264
 
  print same_exponent( '-2','5' )
265
 
 
266
 
  print "same_exponent( '-2.00','5' )    -->",
267
 
  print same_exponent( '-2.00','5' )
 
258
  print("same_exponent( '-2e23','3e24' )    -->",)
 
259
  print(same_exponent( '-2e23','3e24' ))
 
260
 
 
261
  print("same_exponent( '1000','20000' )    -->",)
 
262
  print(same_exponent( '1000','20000' ))
 
263
 
 
264
  print("same_exponent( '-2','5' )    -->",)
 
265
  print(same_exponent( '-2','5' ))
 
266
 
 
267
  print("same_exponent( '-2.00','5' )    -->",)
 
268
  print(same_exponent( '-2.00','5' ))
268
269
 
269
270
def random(interval, verbose = 0):
270
271
  """ random(interval) --> value, latex
272
273
 value is a random number in the interval. The latex is the latex form of value.
273
274
 The value is in an integer form, if it is possible."""
274
275
 
275
 
  if verbose > 0: print "** interval.random() running"
276
 
  if verbose > 1: print ' Interval: %s' % interval
277
 
  num_string1, num_string2 = string.split(interval, '..')
278
 
  num_string1=string.replace(num_string1, 'E', 'e')
279
 
  num_string2=string.replace(num_string2, 'E', 'e')
 
276
  if verbose > 0: print("** interval.random() running")
 
277
  if verbose > 1: print(' Interval: %s' % interval)
 
278
  num_string1, num_string2 = interval.split('..')
 
279
  num_string1 = num_string1.replace('E', 'e')
 
280
  num_string2 = num_string2.replace('E', 'e')
280
281
  factors, exponent, dec_point2, format = same_exponent(num_string1, num_string2)
281
282
 
282
283
  factor1, factor2 = factors
285
286
  else: ratio = 100000
286
287
# It could be logaritmical ... if ratio >10
287
288
  if ratio >= 500:
288
 
    raise ValueError, 'too much range'
 
289
    raise ValueError('too much range')
289
290
 
290
291
 
291
292
  newfactor = randint(factor1, factor2)
305
306
 
306
307
##   try:
307
308
##     if value == int(value):
308
 
##       if verbose: print 'interval.value2latex(): integer'
 
309
##       if verbose: print('interval.value2latex(): integer')
309
310
##       return str(int(value))
310
311
##   except OverflowError:
311
312
##     pass
313
314
  #vstring = '%.4g' % value
314
315
  vstring = '%.8g' % value
315
316
  if 'e' in vstring:
316
 
    vstring = string.replace(vstring, 'e', times + '10^{') + '}'
 
317
    vstring = vstring.replace('e', times + '10^{') + '}'
317
318
  if '.' in vstring and decimal_point != '.':
318
 
    vstring = string.replace(vstring, '.', decimal_point)
 
319
    vstring = vstring.replace('.', decimal_point)
319
320
 
320
 
  latex = string.replace(vstring, '+', '')
 
321
  latex = vstring.replace('+', '')
321
322
  return latex
322
323
 
323
324
def random_test():
324
325
  for intv in ['2.00e16..22e15', '2.00..7', '5000..25000','10..200','-20..9','-2.00e-3..9e-3', '-2.00e-7..9e-7', '1,5e3..6,5e3', '0,1..0,4', '0,12..0,88', '2e-8..16e-8', '2E-8..6E-8']:
325
 
    print '\n****', intv
 
326
    print('\n****', intv)
326
327
    for i in range(3):
327
328
      num, string = random(intv)
328
 
      print 'In interval %s is %s. (LaTeX form: %s)'  % (intv, str(num), string)
 
329
      print('In interval %s is %s. (LaTeX form: %s)'  % (intv, str(num), string))
329
330
 
330
331
##   print
331
332
##   for i in range(20):
332
 
##     print random('0..1'),
 
333
##     print(random('0..1'),)
333
334
 
334
335
##   list = [0]*10
335
336
##   for i in range(1000):
336
337
##     num = random('1..9')
337
338
##     list[num] = list[num] + 1
338
339
##   list.sort()
339
 
##   print list
 
340
##   print(list)
340
341
 
341
342
 
342
343
########################################