content stringlengths 7 1.05M |
|---|
#~ def product(x,y,z=10):
#~ print(x*y*z)
#~ product(2,3)
#~ def product(x=1,y=10,z):
#~ print(x*y*z)
#~ product(2,3)
def product(x,y,z=10):
print(x)
print(y)
print(z)
#~ print(x*y*z)
#~ product(2,3)
product(2,3,100)
|
"""
RPi.Relay
Version: 0.1.0
Homepage: https://github.com/ricco386/RPi/tree/master/RPi.Relay
This software is licensed as described in the README.rst and LICENSE files,
which you should have received as part of this distribution.
"""
__version__ = '0.1.0'
|
class fHDHR_Detect():
def __init__(self, fhdhr):
self.fhdhr = fhdhr
self.fhdhr.db.delete_fhdhr_value("ssdp_detect", "list")
def set(self, location):
detect_list = self.fhdhr.db.get_fhdhr_value("ssdp_detect", "list") or []
if location not in detect_list:
detect_list.append(location)
self.fhdhr.db.set_fhdhr_value("ssdp_detect", "list", detect_list)
def get(self):
return self.fhdhr.db.get_fhdhr_value("ssdp_detect", "list") or []
class Plugin_OBJ():
def __init__(self, fhdhr, plugin_utils, broadcast_ip, max_age):
self.fhdhr = fhdhr
self.detect_method = fHDHR_Detect(fhdhr)
self.broadcast_ip = broadcast_ip
self.device_xml_path = '/cluster/device.xml'
self.schema = "upnp:rootdevice"
self.max_age = max_age
@property
def enabled(self):
return self.fhdhr.config.dict["cluster"]["enabled"]
@property
def notify(self):
data = ''
data_command = "NOTIFY * HTTP/1.1"
data_dict = {
"HOST": "%s:%d" % ("239.255.255.250", 1900),
"NTS": "ssdp:alive",
"USN": 'uuid:%s::%s' % (self.fhdhr.config.dict["main"]["uuid"], self.schema),
"LOCATION": "%s%s" % (self.fhdhr.api.base, self.device_xml_path),
"EXT": '',
"SERVER": 'fHDHR/%s UPnP/1.0' % self.fhdhr.version,
"Cache-Control:max-age=": self.max_age,
"NT": self.schema,
}
data += "%s\r\n" % data_command
for data_key in list(data_dict.keys()):
data += "%s:%s\r\n" % (data_key, data_dict[data_key])
data += "\r\n"
return data
def on_recv(self, headers, cmd, ssdp_handling):
if cmd[0] == 'NOTIFY' and cmd[1] == '*':
try:
if headers["server"].startswith("fHDHR"):
if headers["location"].endswith("/device.xml"):
savelocation = headers["location"].split("/device.xml")[0]
if savelocation.endswith("/cluster"):
savelocation = savelocation.replace("/cluster", '')
if savelocation != self.fhdhr.api.base:
self.detect_method.set(savelocation)
except KeyError:
return
|
# -*- coding: utf-8 -*-
""" VITA Person Finder, Controllers
@author: nursix
@see: U{http://eden.sahanafoundation.org/wiki/BluePrintVITA}
"""
prefix = request.controller
resourcename = request.function
if prefix not in deployment_settings.modules:
session.error = T("Module disabled!")
redirect(URL(r=request, c="default", f="index"))
# -----------------------------------------------------------------------------
def shn_menu():
""" Options menu """
response.menu_options = [
[T("Search for a Person"), False, aURL(r=request, f="index")],
[T("Missing Persons"), False, aURL(r=request, f="person"), [
[T("List"), False, aURL(r=request, f="person")],
[T("Add"), False, aURL(p="create", r=request, f="person", args="create")],
]]]
menu_selected = []
if session.rcvars and "pr_person" in session.rcvars:
person = db.pr_person
query = (person.id == session.rcvars["pr_person"])
record = db(query).select(person.id, limitby=(0, 1)).first()
if record:
name = shn_pr_person_represent(record.id)
menu_selected.append(["%s: %s" % (T("Person"), name), False,
URL(r=request, f="person", args=[record.id])])
if menu_selected:
menu_selected = [T("Open recent"), True, None, menu_selected]
response.menu_options.append(menu_selected)
shn_menu()
# -----------------------------------------------------------------------------
def index():
""" Module's Home Page """
# Module's nice name
try:
module_name = deployment_settings.modules[prefix].name_nice
except:
module_name = T("Missing Persons")
# Override prefix and resourcename
_prefix = "pr"
resourcename = "person"
# Choose table
tablename = "%s_%s" % (_prefix, resourcename)
table = db[tablename]
# Configure redirection and list fields
report_url = URL(r=request, c="pf", f=resourcename,
args=["[id]", "missing_report"])
s3xrc.model.configure(table,
create_next =report_url,
list_fields=["id",
"first_name",
"middle_name",
"last_name",
"gender",
"age_group",
"missing"])
# Pre-process
def prep(r):
""" Redirect to search/person view """
if r.representation == "html":
if not r.id:
r.method = "search"
else:
redirect(URL(r=request, f=resourcename, args=[r.id]))
return True
# Post-process
def postp(r, output):
""" Custom action buttons """
response.s3.actions = []
# Button labels
MISSING = str(T("Missing"))
SEEN = str(T("Seen"))
FOUND = str(T("Found"))
DETAILS = str(T("Details"))
if not r.component:
open_button_label = DETAILS
if auth.s3_logged_in():
# Define URLs
report_missing = str(URL(r=request, f=resourcename,
args=["[id]", "missing_report"]))
#report_seen = str(URL(r=request, f=resourcename,
#args=["[id]", "presence"],
#vars=dict(condition=vita.SEEN)))
report_found = str(URL(r=request, f=resourcename,
args=["[id]", "presence"],
vars=dict(condition=vita.CONFIRMED)))
# Set action buttons
response.s3.actions = [
dict(label=MISSING, _class="action-btn", url=report_missing),
#dict(label=SEEN, _class="action-btn", url=report_seen),
dict(label=FOUND, _class="action-btn", url=report_found),
]
# Is the current user reported missing?
if isinstance(output, dict):
person = s3_logged_in_person()
if person and db.pr_person[person].missing:
myself = URL(r=request, f=resourcename,
args=[person.id, "presence"],
vars=dict(condition=vita.CONFIRMED))
output.update(myself=myself)
else:
open_button_label = UPDATE
# Always have an Open-button
linkto = r.resource.crud._linkto(r, update=True)("[id]")
response.s3.actions.append(dict(label=open_button_label,
_class="action-btn", url=linkto))
return output
# Set hooks
response.s3.prep = prep
response.s3.postp = postp
if auth.s3_logged_in():
add_btn = A(T("Add Person"),
_class="action-btn",
_href=URL(r=request, f="person", args="create"))
else:
add_btn = None
# REST controllerperson
output = s3_rest_controller("pr", "person",
module_name=module_name,
add_btn=add_btn)
# Set view, update menu and return output
response.view = "pf/index.html"
response.title = module_name
shn_menu()
return output
# -----------------------------------------------------------------------------
def person():
""" RESTful CRUD controller """
prefix = "pr"
tablename = "%s_%s" % (prefix, resourcename)
table = db[tablename]
s3.crud_strings[tablename].update(
title_display = T("Missing Person Details"),
title_list = T("Missing Persons Registry"),
subtitle_list = T("Missing Persons"),
label_list_button = T("List Missing Persons"),
msg_list_empty = T("No Persons currently reported missing"))
s3xrc.model.configure(db.pr_group_membership,
list_fields=["id",
"group_id",
"group_head",
"description"])
s3xrc.model.configure(table,
# Redirect to missing report when a new person has been added
create_next = URL(r=request, c="pf", f="person", args=["[id]", "missing_report"]),
list_fields=["id",
"first_name",
"middle_name",
"last_name",
"gender",
"age_group",
"missing"])
def person_prep(r):
# Pre-populate observer fields
person_id = s3_logged_in_person()
if person:
db.pr_presence.observer.default = person_id
db.pr_presence.observer.writable = False
db.pr_presence.observer.comment = None
db.pf_missing_report.observer.default = person_id
db.pf_missing_report.observer.writable = False
db.pf_missing_report.observer.comment = None
# Copy config
if r.component_name == "config":
_config = db.gis_config
defaults = db(_config.id == 1).select(limitby=(0, 1)).first()
for key in defaults.keys():
if key not in ["id", "uuid", "mci", "update_record", "delete_record"]:
_config[key].default = defaults[key]
# Pre-populate presence condition from URL vars
elif r.component_name == "presence":
condition = r.request.vars.get("condition", None)
if condition:
try:
condition = int(condition)
except:
pass
else:
table = db.pr_presence
table.presence_condition.default = condition
table.presence_condition.readable = False
table.presence_condition.writable = False
if condition in vita.PERSISTANT_PRESENCE or \
condition in vita.ABSENCE:
s3xrc.model.configure(table,
mark_required=["location_id", "shelter_id"])
table.orig_id.readable = False
table.orig_id.writable = False
table.dest_id.readable = False
table.dest_id.writable = False
table.observer.readable = False
table.observer.writable = False
return True
response.s3.prep = person_prep
def person_postp(r, output):
# Action buttons
if r.interactive:
if not r.component:
label = READ
linkto = URL(r=request, f="person", args=("[id]", "missing_report"))
else:
label = UPDATE
linkto = s3xrc.crud._linkto(r)("[id]")
response.s3.actions = [
dict(label=str(label), _class="action-btn", url=str(linkto))]
if not r.component:
label = T("Found")
linkto = URL(r=request, f="person",
args=("[id]", "presence"),
vars=dict(condition=vita.CONFIRMED))
response.s3.actions.append(
dict(label=str(label), _class="action-btn", url=str(linkto)))
elif r.component_name == "presence":
if "showadd_btn" in output:
del output["showadd_btn"]
return output
response.s3.postp = person_postp
# Disable missing flag in person
db.pr_person.missing.readable = False
db.pr_person.missing.writable = False
db.pr_person.missing.default = True
# Disable person_id in missing report
db.pf_missing_report.person_id.readable = False
db.pf_missing_report.person_id.writable = False
# Show only missing persons in list views
if len(request.args) == 0:
response.s3.filter = (db.pr_person.missing == True)
# Resource header and tab list
pf_tabs = [(T("Missing Report"), "missing_report"),
(T("Person Details"), None),
(T("Physical Description"), "physical_description"),
(T("Images"), "image"),
(T("Identity"), "identity"),
(T("Address"), "address"),
(T("Contact Data"), "pe_contact"),
(T("Presence Log"), "presence")]
rheader = lambda r: shn_pr_rheader(r, tabs=pf_tabs)
# REST controller
output = s3_rest_controller("pr", resourcename, rheader=rheader)
# Update menu and return output
shn_menu()
return output
# -----------------------------------------------------------------------------
def download():
""" Download a file. """
return response.download(request, db)
# -----------------------------------------------------------------------------
def tooltip():
""" Ajax tooltips """
if "formfield" in request.vars:
response.view = "pr/ajaxtips/%s.html" % request.vars.formfield
return dict()
# -----------------------------------------------------------------------------
|
class Solution:
def checkIfPangram(self, sentence: str) -> bool:
words = set(sentence)
if (len(words) != 26):
return False
else:
return True |
TEXT = '''
<div class="row">
<div class="col-md-12">
<hr/>
<div class="page-footer">
<p class="page-footer"><a href="/legal/privacypolicy" target="_blank">Privacy Policy <i class='fa fa-pencil' style='font-size:16px;color:red'></i></a></p>
<p class="page-footer">Copyright © theblueplanet.net 2019 all rights reserved, except content provided by third parties.</p>
<p class="page-footer">We use cookies to enhance and improve our services. By using this site you agree to our <a href="/legal/cookies" target="_blank">use of cookies</a>.</p></div>
</div>
</div>''' |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for `pyxq` package."""
def test_000_something():
"""Test something."""
|
class Dataset:
def __init__(self):
self.dataset = None
self.groundtruth = None
self.camera_file = None
self.camera = None
self.quat = None
self.init_pose = None
self.rgb_image = None
self.pre_assoc_file_path = None
self.descr = None
|
f = str(input('Digite uma frase: ')).strip().upper()
p = f.split()
j = ''.join(p)
#i = ''
i = j[::-1]
#for l in range(len(j)-1,-1,-1):
# i += j[l]
print('o inverso de {} é {}.'.format(j,i))
if i == j:
print('Temos um palíndromo!')
else:
print('A frase digitada não é um palíndromo.') |
"""
Module: 'uhashlib' on LEGO EV3 v1.0.0
"""
# MCU: sysname=ev3, nodename=ev3, release=('v1.0.0',), version=('0.0.0',), machine=ev3
# Stubber: 1.3.2
class sha1:
''
def digest():
pass
def update():
pass
class sha256:
''
def digest():
pass
def update():
pass
|
def policy(resource):
if resource['MaxPasswordAge'] is None:
return False
return resource['MaxPasswordAge'] <= 90
|
class Solution(object):
def multiply(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: List[List[int]]
"""
if A is None or B is None or len(A) == 0 or len(B) == 0:
return [[]]
m = len(A)
n = len(B[0])
res = [[0 for _ in range(n)] for _ in range(m)]
sparseB = [[] for _ in range(len(B))]
for i in range(len(B)):
for j in range(len(B[i])):
sparseB[i].append((j, B[i][j]))
for i in range(m):
for k in range(len(A[i])):
if A[i][k] != 0:
for p in range(len(sparseB[k])):
j, val = sparseB[k][p]
res[i][j] += A[i][k] * val
return res |
"""
Define the exception classes for aioharmony module.
"""
class HarmonyException(Exception):
"""Top level Harmony Exception"""
class HarmonyClient(HarmonyException):
"""
Top level exception for HarmonyClient
"""
class TimeOut(HarmonyClient, TimeoutError):
"""
Raised on timeouts
"""
|
def cubicip(ipparams, position, etc = []):
"""
This function fits the intra-pixel sensitivity effect using a 2D cubic.
Parameters
----------
a: cubic coefficient in y
b: cubic coefficient in x
c: coefficient of cross-term xy^2
d: coefficient of cross-term yx^2
e: quadratic coefficient in y
f: quadratic coefficient in x
g: coefficient of cross-term xy
h: linear coefficient in y
i: linear coefficient in x
j: constant
Returns
-------
returns the flux values for the intra-pixel model
Revisions
---------
2008-07-08 Kevin Stevenson, UCF
kevin218@knights.ucf.edu
Original version
"""
a = ipparams[0]
b = ipparams[1]
c = ipparams[2]
d = ipparams[3]
e = ipparams[4]
f = ipparams[5]
g = ipparams[6]
h = ipparams[7]
i = ipparams[8]
j = ipparams[9]
y, x, q = position
return a*y**3 + b*x**3 + c*y**2*x + d*y*x**2 + e*y**2 + f*x**2 + g*y*x + h*y + i*x + j
|
"""
Initial jokes from stackoverflow - provided under CC BY-SA 3.0
http://stackoverflow.com/questions/234075/what-is-your-best-programmer
-joke?page=4&tab=votes#tab-top
Copied from pyjokes.
The purpose of this is so users without pyjokes pip installed can run
select_a_scrum_master.py
"""
scrum_chooser_jokes = [
"Complaining about the lack of smoking shelters, the nicotine addicted "
"Python programmers said there ought to be 'spaces for tabs'.",
"Ubuntu users are apt to get this joke.",
"Where should one store all the nerdy dad jokes? In a dada-base.",
"An SQL query goes into a bar, walks up to two tables and asks, 'Can I "
"join you?'",
"If you put a million monkeys at a million keyboards, one of them will "
"eventually write a Java program. The rest of them will write Perl.",
"I suggested holding a 'Python Object Oriented Programming Seminar', but "
"the acronym was unpopular.",
"How many programmers does it take to change a lightbulb? None, that's a "
"hardware problem.",
"What's the object-oriented way to become wealthy? Inheritance.",
"How many programmers does it take to change a lightbulb? None, they just "
"make darkness a standard.",
"Two bytes meet. The first byte asks, 'Are you ill?' The second byte "
"replies, 'No, just feeling a bit off.'",
"Two threads walk into a bar. The barkeeper looks up and yells, 'Hey, I "
"want don't any conditions race like time last!'",
"Old C programmers don't die, they're just cast into void.",
"Eight bytes walk into a bar. The bartender asks, 'Can I get you "
"anything?' 'Yeah,' replies the bytes. 'Make us a double.'",
"Why did the programmer quit his job? Because he didn't get arrays.",
"Why do Java programmers have to wear glasses? Because they don't see "
"sharp.",
"Software developers like to solve problems. If there are no problems "
"handily available, they will create their own.",
".NET was named .NET so that it wouldn't show up in a Unix directory "
"listing.",
"Hardware: The part of a computer that you can kick.",
"A programmer was found dead in the shower. Next to their body was a "
"bottle of shampoo with the instructions 'Lather, Rinse and Repeat'.",
"Optimist: The glass is half full. Pessimist: The glass is half empty. "
"Programmer: The glass is twice as large as necessary.",
"In C we had to code our own bugs. In C++ we can inherit them.",
"How come there is no obfuscated Perl contest? Because everyone would "
"win.",
"If you play a Windows CD backwards, you'll hear satanic chanting ... "
"worse still, if you play it forwards, it installs Windows.",
"How many programmers does it take to kill a cockroach? Two: one holds, "
"the other installs Windows on it.",
"What do you call a programmer from Finland? Nerdic.",
"What did the Java code say to the C code? A: You've got no class.",
"Why did Microsoft name their search engine BING? Because It's Not "
"Google.",
"Pirates go 'arg!', computer pirates go 'argv!'",
"Software salesmen and used-car salesmen differ in that the latter know "
"when they are lying.",
"Why do programmers confuse Halloween with Christmas? Because OCT 31 == "
"DEC 25.",
"How many Prolog programmers does it take to change a lightbulb? false.",
"Real programmers can write assembly code in any language.",
"Waiter: Would you like coffee or tea? Programmer: Yes.",
"If loving you is ROM I don't wanna read write.",
"A programmer walks into a foo...",
"A programmer walks into a bar and orders 1.38 root beers. The bartender "
"informs her it's a root beer float. She says 'Make it a double!'",
"Why are you always smiling? That's just my... regular expression.",
"ASCII stupid question, get a stupid ANSI.",
"A programmer had a problem. He thought to himself, 'I know, I'll solve it"
" with threads!'. has Now problems. two he",
"Why do sin and tan work? Just cos.",
"Java: Write once, run away.",
"I would tell you a joke about UDP, but you would never get it.",
"A QA engineer walks into a bar. Runs into a bar. Crawls into a bar. "
"Dances into a bar. Tiptoes into a bar. Rams a bar. Jumps into a bar.",
"My friend's in a band called '1023 Megabytes'... They haven't got a gig "
"yet!",
"I had a problem so I thought I'd use Java. Now I have a ProblemFactory.",
"QA Engineer walks into a bar. Orders a beer. Orders 0 beers. Orders "
"999999999 beers. Orders a lizard. Orders -1 beers. Orders a sfdeljknesv.",
"A product manager walks into a bar, asks for drink. Bartender says no, "
"but will consider adding later.",
"How do you generate a random string? Put a first year Computer Science "
"student in Vim and ask them to save and exit.",
"I've been using Vim for a long time now, mainly because I can't figure "
"out how to exit.",
"How do you know whether a person is a Vim user? Don't worry, they'll tell"
" you.",
"Waiter: He's choking! Is anyone a doctor? Programmer: I'm a Vim user.",
"3 Database Admins walked into a NoSQL bar. A little while later they "
"walked out because they couldn't find a table.",
"How to explain the movie Inception to a programmer? When you run a VM "
"inside another VM, inside another VM ... everything runs real slow!",
"There are only two hard problems in Computer Science: cache invalidation,"
" naming things and off-by-one-errors.",
"There are 10 types of people: those who understand binary and those who "
"don't.",
"There are 2 types of people: those who can extrapolate from incomplete "
"data sets...",
"There are II types of people: Those who understand Roman Numerals and "
"those who don't.",
"There are 10 types of people: those who understand hexadecimal and 15 "
"others.",
"There are 10 types of people: those who understand binary, those who "
"don't, and those who were expecting this joke to be in trinary.",
"There are 10 types of people: those who understand trinary, those who "
"don't, and those who have never heard of it.",
"What do you call eight hobbits? A hobbyte.",
"The best thing about a Boolean is even if you are wrong, you are only "
"off by a bit.",
"A good programmer is someone who always looks both ways before crossing "
"a one-way street.",
"There are two ways to write error-free programs; only the third one "
"works.",
"QAs consist of 55% water, 30% blood and 15% Jira tickets.",
"How many QAs does it take to change a lightbulb? They noticed that the "
"room was dark. They don't fix problems, they find them.",
"A programmer crashes a car at the bottom of a hill, a bystander asks what"
" happened, he says \"No idea. Let's push it back up and try again\".",
"What do you mean 911 is only for emergencies? I've got a merge conflict.",
"Writing PHP is like peeing in the swimming pool, everyone did it, but we"
" don't need to bring it up in public.",
"Why did the QA cross the road? To ruin everyone's day.",
"Number of days since I have encountered an array index error: -1.",
"Number of days since I have encountered an off-by-one error: 0.",
"Speed dating is useless. 5 minutes is not enough to properly explain the"
" benefits of the Unix philosophy.",
"Microsoft hold a bi-monthly internal \"productive week\" where they use "
"Google instead of Bing.",
"Schrodinger's attitude to web development: If I don't look at it in "
"Internet Explorer then there's a chance it looks fine.",
"Finding a good PHP developer is like looking for a needle in a haystack. "
"Or is it a hackstack in a needle?",
"Unix is user friendly. It's just very particular about who its friends "
"are.",
"The C language combines all the power of assembly language with all the "
"ease-of-use of assembly language.",
"What does 'Emacs' stand for? 'Exclusively used by middle aged computer "
"scientists.'",
"What does pyjokes have in common with Adobe Flash? It gets updated all "
"the time, but never gets any better.",
"Why does Waldo only wear stripes? Because he doesn't want to be spotted.",
"I went to a street where the houses were numbered 8k, 16k, 32k, 64k, "
"128k, 256k and 512k. It was a trip down Memory Lane.",
"!false, (It's funny because it's true)",
"['hip', 'hip'] (hip hip array!)",
]
# Program is only meant to be imported
if __name__ == "main":
raise SystemExit
|
# =============================================================================
# Fog Metrics Utilities
# =============================================================================
#
# Miscellaneous utility functions used to compute metrics
#
ACCEPTABLE_TYPES = (set, frozenset, dict)
def intersection_size(A, B):
if A is B:
return len(A)
if not isinstance(A, ACCEPTABLE_TYPES):
A = set(A)
if not isinstance(B, ACCEPTABLE_TYPES):
B = set(B)
if len(A) > len(B):
A, B = B, A
if len(A) == 0:
return 0
i = 0
for x in A:
if x in B:
i += 1
return i
|
def test(r):
avg = round(sum(r) / len(r), 3)
hal = { "h": 0, "a": 0, "l": 0 }
for mark in r:
if mark > 8:
hal["h"] += 1
elif mark > 6:
hal["a"] += 1
else:
hal["l"] += 1
return [avg, hal, "They did well"] if hal["h"] == len(r) else [avg, hal]
|
#desenvolva um programa que leia as duas notas de um aluno, calcule e mostre a sua média
n1 = float(input('Digite nota do primeiro bimestre '))
n2 = float(input('Digite nota do segundo bimestre '))
n3 = float(input('Digite nota do terceiro bimestre '))
n4 = float(input('Digite nota do quarto bimestre '))
s = n1 + n2 + n3 + n4
print("Sua média ficou {:.1f} ".format(s / 4)) |
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
'''
This module generates ANSI character codes to printing colors to terminals.
See: http://en.wikipedia.org/wiki/ANSI_escape_code
'''
CSI = '\033['
OSC = '\033]'
BEL = '\007'
def code_to_chars(code):
return CSI + str(code) + 'm'
def set_title(title):
return OSC + '2;' + title + BEL
def clear_screen(mode=2):
return CSI + str(mode) + 'J'
def clear_line(mode=2):
return CSI + str(mode) + 'K'
# This function needs to be able to take ANY given ANSI Color String and return
# It's value as the next incremented color code.
def increment_color(code):
""" Takes a Given Color Code Defined in a Subclass of AnsiCodes,
and returns the next modulating non-reseting color.
:param code: ANSI Color Constant defined in either Fore, Back, or Style.
These Color Constants come in the form '\033['+ X + 'm', where X is the actual integer that we want.
:type code: str
:return: Another ANSI Color Constant defined in the corresponding subclasses
:rtype: str
"""
# Extract the number from the code, this strips all the strings away from the number,
# And allows us to simply convert from string to integer directly
color_num = int(code.lstrip(CSI).rstrip('m'))
# Fore Integers are defined 30-37, 90-97. 39 is a reset value, and 38 is a blank
# Back Integers are defined 40-47, 100-107, 49 is a reset value, and 48 is also a blank
# Style Integers are defined as 1,2 & 22. 0 is a reset value.
# We want the Style, Fore, and Back integers to stay in their respective class,
# Which eliminates the possibility of using a circular buffer.
# Instead we have to use this spaghetti code logic in order to handle the fringe cases
# Ensuring that we're incrementing when needed and decrementing to the correct positions.
# Handle the fringe cases for the Fore and Back ANSI codes to jump to the non-standard codes
color_num = color_num + 53 if color_num == 47 or color_num == 37 else\
color_num - 67 if color_num == 107 or color_num == 97 else\
color_num + 1 if color_num >= 30 and color_num != (39 and 49) else\
1 if color_num == 22 else\
2 if color_num == 1 else\
22 if color_num == 2 else\
1 if color_num == 0 else color_num + 51
# At this point, 39 & 49 would be the only ones left
# So we just add 51 to them bringing them up to 90 or 100
# Return it as a string prepended with CSI and appended with 'm'
return CSI + str(color_num) + 'm'
class AnsiCodes(object):
def __init__(self):
# the subclasses declare class attributes which are numbers.
# Upon instantiation we define instance attributes, which are the same
# as the class attributes but wrapped with the ANSI escape sequence
for name in dir(self):
if not name.startswith('_'):
value = getattr(self, name)
setattr(self, name, code_to_chars(value))
class AnsiCursor(object):
def UP(self, n=1):
return CSI + str(n) + 'A'
def DOWN(self, n=1):
return CSI + str(n) + 'B'
def FORWARD(self, n=1):
return CSI + str(n) + 'C'
def BACK(self, n=1):
return CSI + str(n) + 'D'
def POS(self, x=1, y=1):
return CSI + str(y) + ';' + str(x) + 'H'
class AnsiFore(AnsiCodes):
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
RESET = 39
# These are fairly well supported, but not part of the standard.
LIGHTBLACK_EX = 90
LIGHTRED_EX = 91
LIGHTGREEN_EX = 92
LIGHTYELLOW_EX = 93
LIGHTBLUE_EX = 94
LIGHTMAGENTA_EX = 95
LIGHTCYAN_EX = 96
LIGHTWHITE_EX = 97
class AnsiBack(AnsiCodes):
BLACK = 40
RED = 41
GREEN = 42
YELLOW = 43
BLUE = 44
MAGENTA = 45
CYAN = 46
WHITE = 47
RESET = 49
# These are fairly well supported, but not part of the standard.
LIGHTBLACK_EX = 100
LIGHTRED_EX = 101
LIGHTGREEN_EX = 102
LIGHTYELLOW_EX = 103
LIGHTBLUE_EX = 104
LIGHTMAGENTA_EX = 105
LIGHTCYAN_EX = 106
LIGHTWHITE_EX = 107
class AnsiStyle(AnsiCodes):
BRIGHT = 1
DIM = 2
NORMAL = 22
RESET_ALL = 0
Fore = AnsiFore()
Back = AnsiBack()
Style = AnsiStyle()
Cursor = AnsiCursor()
|
print("import getmac")
def get_mac_address(interface=None, ip=None, ip6=None, hostname=None, network_request=True):
get_mac_address_content = f"{f'interface={interface}' if interface else ''}{f', ip={ip}' if ip else ''}{f', ip6={ip6}' if ip6 else ''}{f', hostname={hostname}' if hostname else ''}{f', network_request={network_request}' if network_request and not network_request == True else ''}"
print(f'getmac.get_mac_address({get_mac_address_content})')
return "00:00:00:00:00:00" |
_base_ = '../../../base.py'
# model settings
model = dict(
type='Classification',
pretrained=None,
backbone=dict(
type='ResNet',
depth=50,
in_channels=3,
out_indices=[4], # 0: conv-1, x: stage-x
# TODO(cjrd) should we be using BN here???
norm_cfg=dict(type='BN')),
head=dict(
type='ClsHead', with_avg_pool=True, in_channels=2048,
num_classes=10))
# dataset settings
data_source_cfg = dict(
type='ImageNet',
memcached=False,
mclient_path='/no/matter')
data_train_list = "data/imagenet/meta/train_1000.txt"
data_train_root = 'data/imagenet'
data_val_list = "data/imagenet/meta/val_1000.txt"
data_val_root = 'data/imagenet'
data_test_list = "data/imagenet/meta/test_1000.txt"
data_test_root = 'data/imagenet'
dataset_type = 'ClassificationDataset'
img_norm_cfg = dict(mean=[0.5,0.6,0.7], std=[0.1,0.2,0.3])
train_pipeline = [
dict(type='RandomResizedCrop', size=224),
dict(type='RandomHorizontalFlip'),
dict(type='ToTensor'),
dict(type='Normalize', **img_norm_cfg),
]
test_pipeline = [
dict(type='Resize', size=256),
dict(type='CenterCrop', size=224),
dict(type='ToTensor'),
dict(type='Normalize', **img_norm_cfg),
]
data = dict(
imgs_per_gpu=128, # total 512
workers_per_gpu=4,
train=dict(
type=dataset_type,
data_source=dict(
list_file=data_train_list, root=data_train_root,
**data_source_cfg),
pipeline=train_pipeline),
val=dict(
type=dataset_type,
data_source=dict(
list_file=data_val_list, root=data_val_root, **data_source_cfg),
pipeline=test_pipeline),
test=dict(
type=dataset_type,
data_source=dict(
list_file=data_test_list, root=data_test_root, **data_source_cfg),
pipeline=test_pipeline))
prefetch=False
|
# Depth-first search
def dfs(node, explored):
if node not in explored:
explored.append(node)
for n in node.neighbors:
dfs(n, explored)
return explored
# Breadth-first search
def bfs(start, goal):
# begin = time()
explored = []
paths = [[start]]
while len(paths) > 0:
path1 = paths.pop(0)
lastnode = path1[-1]
if lastnode not in explored:
for n in lastnode.neighbors:
newpath = list(path1)
newpath.append(n)
paths.append(newpath)
if n == goal:
# print(time() - begin)
return newpath
explored.append(lastnode)
# pass
|
def counter(start=0):
n = start
while True:
yield n
n += 1
c = counter()
print(next(c)) # prints: 0
print(next(c)) # prints: 1
print(next(c)) # prints: 2
|
"""
Reindex the purchase records DataFrame to be indexed hierarchally, first by store,
then by person. Name these indexes 'Location'
and 'Name'. Then add a new entry.
"""
purchase_1 = pd.Series({'Name': 'Chris',
'Item Purchased': 'Dog Food',
'Cost': 22.50})
purchase_2 = pd.Series({'Name': 'Kevyn',
'Item Purchased': 'Kitty Litter',
'Cost': 2.50})
purchase_3 = pd.Series({'Name': 'Vinod',
'Item Purchased': 'Bird Seed',
'Cost': 5.00})
df = pd.DataFrame([purchase_1, purchase_2, purchase_3], index=['Store 1', 'Store 1', 'Store 2'])
df = df.set_index([df.index, 'Name'])
df.index.names = ['Location', 'Name']
df = df.append(pd.Series(data={'Cost': 3.00, 'Item Purchased': 'Kitty Food'}, name=('Store 2', 'Kevyn')))
df
|
"""
Use null statement as placeholder for future implementation
-----------------------------------------------------------
Python interpreter doesn't ignore pass. The difference between pass and comment,
that interpreter ignores comments. Pass is unll statement that means it is a
general palceholder for future implementation of functions, loops, classes.
Using pass help avoid the runtime errors and the code will be runable.
"""
print('This is the beginning of example')
# Using pass prevent running the for loop.
for i in range(4):
pass
print('\nEnd of examples') |
# list can store any type of data type.
temperatures = []
# add at the end.
temperatures.append(98.6)
temperatures.append(98.7)
print(temperatures)
er_temps = [102.2, 103.5]
# extends the original list.
temperatures.extend(er_temps)
print(temperatures)
# Concatenate
primary_care_doctors = ["Dr. Scholls", "Dr. Pepper"]
er_doctor = ["Doug", "Susan"]
all_doctors = primary_care_doctors + er_doctor
print(all_doctors)
hello = ["How", "are", "you"]
hello.extend("hello")
print(hello)
|
class Solution:
def traverse(self, l, dep):
for e in l:
if e.isInteger():
v = e.getInteger()
self.sumdep += dep * v
else:
self.traverse(e.getList(), dep + 1)
def depthSum(self, nestedList: List[NestedInteger]) -> int:
self.sumdep = 0
self.traverse(nestedList, 1)
return self.sumdep
|
#
# @lc app=leetcode id=538 lang=python3
#
# [538] Convert BST to Greater Tree
#
# https://leetcode.com/problems/convert-bst-to-greater-tree/description/
#
# algorithms
# Medium (56.64%)
# Likes: 2561
# Dislikes: 144
# Total Accepted: 163.3K
# Total Submissions: 273.4K
# Testcase Example: '[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]'
#
# Given the root of a Binary Search Tree (BST), convert it to a Greater Tree
# such that every key of the original BST is changed to the original key plus
# sum of all keys greater than the original key in BST.
#
# As a reminder, a binary search tree is a tree that satisfies these
# constraints:
#
#
# The left subtree of a node contains only nodes with keys less than the node's
# key.
# The right subtree of a node contains only nodes with keys greater than the
# node's key.
# Both the left and right subtrees must also be binary search trees.
#
#
# Note: This question is the same as 1038:
# https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
#
#
# Example 1:
#
#
# Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
# Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
#
#
# Example 2:
#
#
# Input: root = [0,null,1]
# Output: [1,null,1]
#
#
# Example 3:
#
#
# Input: root = [1,0,2]
# Output: [3,3,2]
#
#
# Example 4:
#
#
# Input: root = [3,2,4,1]
# Output: [7,9,4,10]
#
#
#
# Constraints:
#
#
# The number of nodes in the tree is in the range [0, 10^4].
# -10^4 <= Node.val <= 10^4
# All the values in the tree are unique.
# root is guaranteed to be a valid binary search tree.
#
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def convertBST(self, root: TreeNode) -> TreeNode:
new_root, _ = self.convert(root, 0)
return new_root
def convert(self, root, incre):
if not root:
return root, incre
right, right_max = self.convert(root.right, incre)
new_root = TreeNode(root.val + right_max)
left, left_max = self.convert(root.left, new_root.val)
new_root.left = left
new_root.right = right
return new_root, left_max
# @lc code=end
|
class _DataPad:
data_types = ['video', 'audio', 'subtitles', 'data']
def __init__(self):
self.__data_type = None
self.media_stream = None
@property
def data_type(self):
return self.__data_type
@data_type.setter
def data_type(self, dt):
if dt in _DataPad.data_types:
self.__data_type = dt
else:
raise ValueError('Data type {} isn\'t allowed'.format(dt))
class VideoDataPad(_DataPad):
def __init__(self):
super().__init__()
self.data_type = 'video'
class AudioDataPad(_DataPad):
def __init__(self):
super().__init__()
self.data_type = 'audio'
class SubtitleDataPad(_DataPad):
def __init__(self):
super().__init__()
self.data_type = 'subtitle'
class ArbitraryDataPad(_DataPad):
def __init__(self):
super().__init__()
self.data_type = 'data'
class _InputNode:
def __init__(self, media_file=None, template=None):
pass
|
class Solution(object):
def maxSubArray(self, nums):
max_sum=float('-inf')
cur_sum=0
for i in range(len(nums)):
cur_sum += nums[i]
if cur_sum>max_sum:
max_sum = cur_sum
if cur_sum<0:
cur_sum = 0
return max_sum
|
# Problem: https://www.hackerrank.com/challenges/s10-the-central-limit-theorem-3/problem
# Score: 30
# inputs
mean = 500
std = 80
n = 100
z = 1.96
# characteristics of sample
mean = mean
std = std / n**(1/2)
# Find the 95% interval
print(round(mean - std * z, 2))
print(round(mean + std * z, 2))
|
class Solution:
def validateStackSequences(self, pushed, popped):
"""
:type pushed: List[int]
:type popped: List[int]
:rtype: bool
"""
stack = []
pushed_index, popped_index = 0, 0
while pushed_index < len(pushed) or popped_index < len(popped):
if len(stack) > 0 and popped_index < len(popped) and stack[-1] == popped[popped_index]:
stack.pop()
popped_index += 1
elif pushed_index < len(pushed):
stack.append(pushed[pushed_index])
pushed_index += 1
else:
return False
return True
if __name__ == "__main__":
print(Solution().validateStackSequences([1, 2, 3, 4, 5], [4, 5, 3, 2, 1]))
print(Solution().validateStackSequences([1, 2, 3, 4, 5], [4, 3, 5, 1, 2]))
|
# Portions Copyright (c) 2005 Nokia Corporation
#
# Secret Labs' Regular Expression Engine
#
# various symbols used by the regular expression engine.
# run this script to update the _sre include files!
#
# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
#
# See the sre.py file for information on usage and redistribution.
#
"""Internal support module for sre"""
# update when constants are added or removed
MAGIC = 20010701
# max code word in this release
MAXREPEAT = 65535
# SRE standard exception (access as sre.error)
# should this really be here?
class error(Exception):
pass
# operators
FAILURE = "failure"
SUCCESS = "success"
ANY = "any"
ANY_ALL = "any_all"
ASSERT = "assert"
ASSERT_NOT = "assert_not"
AT = "at"
BIGCHARSET = "bigcharset"
BRANCH = "branch"
CALL = "call"
CATEGORY = "category"
CHARSET = "charset"
GROUPREF = "groupref"
GROUPREF_IGNORE = "groupref_ignore"
IN = "in"
IN_IGNORE = "in_ignore"
INFO = "info"
JUMP = "jump"
LITERAL = "literal"
LITERAL_IGNORE = "literal_ignore"
MARK = "mark"
MAX_REPEAT = "max_repeat"
MAX_UNTIL = "max_until"
MIN_REPEAT = "min_repeat"
MIN_UNTIL = "min_until"
NEGATE = "negate"
NOT_LITERAL = "not_literal"
NOT_LITERAL_IGNORE = "not_literal_ignore"
RANGE = "range"
REPEAT = "repeat"
REPEAT_ONE = "repeat_one"
SUBPATTERN = "subpattern"
# positions
AT_BEGINNING = "at_beginning"
AT_BEGINNING_LINE = "at_beginning_line"
AT_BEGINNING_STRING = "at_beginning_string"
AT_BOUNDARY = "at_boundary"
AT_NON_BOUNDARY = "at_non_boundary"
AT_END = "at_end"
AT_END_LINE = "at_end_line"
AT_END_STRING = "at_end_string"
AT_LOC_BOUNDARY = "at_loc_boundary"
AT_LOC_NON_BOUNDARY = "at_loc_non_boundary"
AT_UNI_BOUNDARY = "at_uni_boundary"
AT_UNI_NON_BOUNDARY = "at_uni_non_boundary"
# categories
CATEGORY_DIGIT = "category_digit"
CATEGORY_NOT_DIGIT = "category_not_digit"
CATEGORY_SPACE = "category_space"
CATEGORY_NOT_SPACE = "category_not_space"
CATEGORY_WORD = "category_word"
CATEGORY_NOT_WORD = "category_not_word"
CATEGORY_LINEBREAK = "category_linebreak"
CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
CATEGORY_LOC_WORD = "category_loc_word"
CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
CATEGORY_UNI_DIGIT = "category_uni_digit"
CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
CATEGORY_UNI_SPACE = "category_uni_space"
CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
CATEGORY_UNI_WORD = "category_uni_word"
CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
OPCODES = [
# failure=0 success=1 (just because it looks better that way :-)
FAILURE, SUCCESS,
ANY, ANY_ALL,
ASSERT, ASSERT_NOT,
AT,
BRANCH,
CALL,
CATEGORY,
CHARSET, BIGCHARSET,
GROUPREF, GROUPREF_IGNORE,
IN, IN_IGNORE,
INFO,
JUMP,
LITERAL, LITERAL_IGNORE,
MARK,
MAX_UNTIL,
MIN_UNTIL,
NOT_LITERAL, NOT_LITERAL_IGNORE,
NEGATE,
RANGE,
REPEAT,
REPEAT_ONE,
SUBPATTERN
]
ATCODES = [
AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY,
AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING,
AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY,
AT_UNI_NON_BOUNDARY
]
CHCODES = [
CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
CATEGORY_UNI_NOT_LINEBREAK
]
def makedict(list):
d = {}
i = 0
for item in list:
d[item] = i
i = i + 1
return d
OPCODES = makedict(OPCODES)
ATCODES = makedict(ATCODES)
CHCODES = makedict(CHCODES)
# replacement operations for "ignore case" mode
OP_IGNORE = {
GROUPREF: GROUPREF_IGNORE,
IN: IN_IGNORE,
LITERAL: LITERAL_IGNORE,
NOT_LITERAL: NOT_LITERAL_IGNORE
}
AT_MULTILINE = {
AT_BEGINNING: AT_BEGINNING_LINE,
AT_END: AT_END_LINE
}
AT_LOCALE = {
AT_BOUNDARY: AT_LOC_BOUNDARY,
AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
}
AT_UNICODE = {
AT_BOUNDARY: AT_UNI_BOUNDARY,
AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
}
CH_LOCALE = {
CATEGORY_DIGIT: CATEGORY_DIGIT,
CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
CATEGORY_SPACE: CATEGORY_SPACE,
CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
CATEGORY_WORD: CATEGORY_LOC_WORD,
CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
}
CH_UNICODE = {
CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
CATEGORY_SPACE: CATEGORY_UNI_SPACE,
CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
CATEGORY_WORD: CATEGORY_UNI_WORD,
CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
}
# flags
SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
SRE_FLAG_IGNORECASE = 2 # case insensitive
SRE_FLAG_LOCALE = 4 # honour system locale
SRE_FLAG_MULTILINE = 8 # treat target as multiline string
SRE_FLAG_DOTALL = 16 # treat target as a single string
SRE_FLAG_UNICODE = 32 # use unicode locale
SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
SRE_FLAG_DEBUG = 128 # debugging
# flags for INFO primitive
SRE_INFO_PREFIX = 1 # has prefix
SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
SRE_INFO_CHARSET = 4 # pattern starts with character from given set
|
##-------------------------------------------------------------------
"""
Given a binary tree, return the level order traversal of
its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
##-------------------------------------------------------------------
"""
def level_order(root):
ans = []
if not root:
return ans
level = [root]
while level:
current = []
new_level = []
for node in level:
current.append(node.val)
if node.left:
new_level.append(node.left)
if node.right:
new_level.append(node.right)
level = new_level
ans.append(current)
return ans
|
def pytest_addoption(parser):
parser.addoption("--core_url", action="store", default="localhost:6565")
parser.addoption("--serving_url", action="store", default="localhost:6566")
parser.addoption("--jobcontroller_url", action="store", default="localhost:6570")
parser.addoption("--allow_dirty", action="store", default="False")
parser.addoption(
"--gcs_path", action="store", default="gs://feast-templocation-kf-feast/"
)
parser.addoption("--enable_auth", action="store", default="False")
parser.addoption("--kafka_brokers", action="store", default="localhost:9092")
|
class Cab:
def __init__(self,kms,type_of_cab,year):
self.__kms = kms
self.__type_of_cab = type_of_cab
self.__year = year
def get_kms(self):
"""
Returns kms
Parameters:
self
returns:
int: return self.__hms
"""
return self.__kms
def get_type_of_car(self):
"""
Returns type of car
Parameters:
self
returns:
int: return self.__type_of_car
"""
return self.__type_of_cab
def get_year(self):
"""
Returns year
Parameters:
self
returns:
int: return self.__year
"""
return self.__year
def __gt__(self,other):
return self.__year > other.get_year()
def __eq__(self,other):
return self.__year == other.get_year() and self.__type_of_cab == other.get_type_of_car()
def __repr__(self):
print(f"kms: {self.__kms} type of car {self.__type_of_cab} year : {self.__year}")
class Sedan(Cab):
def __init__(self,kms,type_of_cab,year):
Cab.__init__(self,kms,type_of_cab,year)
self.__price_per_km = 2.5
def calculate_fare(self):
"""
Calculation of Fare
Parameters:
self
returns:
int: return Cab.get_kms(self) * self.__price_per_km
"""
return Cab.get_kms(self) * self.__price_per_km
class Hatchback(Cab):
def __init__(self,kms,type_of_cab,year):
Cab.__init__(self,kms,type_of_cab,year)
self.__price_per_km = 2.2
def calculate_fare(self):
"""
Calculation of Fare
Parameters:
self
returns:
int: return Cab.get_kms(self) * self.__price_per_km
"""
return Cab.get_kms(self) * self.__price_per_km
|
"""
Fourth algorithm function for application.
Fill in your own algorithmic code here.
"""
def calculate4(self):
'Calculate ROI and other params given historical stock data using ?? algorithm.'
return False
|
class Armor:
name = ""
info = ""
level = 0
material = ""
heaviness = 0
loudness = 0
hit_points = 10000
cut_damage_reduction = 0 # 1 - 10(%)
stab_damage_reduction = 0 # 1 - 10(%)
smash_damage_reduction = 0 # 1 - 10(%)
special_abilities = []
occupied = False
no_armor = Armor()
# armor patterns
class ChainmailHauberk(Armor):
name = "kroužkový hauberk"
info = "lidmi vyrobená kroužková košile"
level = 2
material = "chaimail"
heaviness = 4
loudness = 7 # 1 - 9
hit_points = 5000
cut_damage_reduction = 3 # 1 - 10(%)
stab_damage_reduction = 3 # 1 - 10(%)
smash_damage_reduction = 4 # 1 - 10(%)
special_abilities = []
class LeatherTunic(Armor):
name = "kožená tunika"
info = "z kůže vyrobená vesta, je celkem tichá a dobře se v ní hýbe, ale tělo moc neochrání"
level = 1
material = "leather"
heaviness = 1
loudness = 2 # 1 - 9
hit_points = 3000
cut_damage_reduction = 1 # 1 - 10(%)
stab_damage_reduction = 1 # 1 - 10(%)
smash_damage_reduction = 3 # 1 - 10(%)
special_abilities = []
class SteelBrestplate(Armor):
pass
class GromrilBrestplate(Armor):
pass
# armors
class Armor1(ChainmailHauberk):
pass
class Armor2(ChainmailHauberk):
pass
class Armor3(LeatherTunic):
pass
class Armor4(LeatherTunic):
pass
# armors
armor_1 = Armor1()
armor_2 = Armor2()
armor_3 = Armor3()
armor_4 = Armor4()
|
def count_change(money, coins):
# your implementation here
coins.sort()
result = 0
cache = [coins[0]]
for i,coin in enumerate(coins):
while sum(cache) < money:
if sum(cache) == money:
result += 1
cache.append(coin)
|
# -*- coding: utf-8 -*-
def main():
n = int(input())
st = [0 for _ in range(n)]
for i in range(n):
si, ti = map(int, input().split())
st[i] = (si, ti)
time = sorted(st, key=lambda x: x[0])
ans = 1
last = time[0][1]
for j in range(1, n):
last = max(last, time[j - 1][1])
if last < time[j][0]:
ans += 1
print(ans)
if __name__ == '__main__':
main()
|
class AzEntity(object):
"""Base class for Azurelib objects"""
def __init__(self, service, name):
"""Creates an Azurelib AzEntity
Args:
service: either a azure.storage.TableService
or a azure.storage.QueueService
name: entity's name. May be None
"""
super(AzEntity, self).__init__()
self._service = service
self.select(name)
def select(self, name):
"""Sets the entity's name. Required for
operations on live Azure instances"""
if name:
name = name.replace("_", "")
# from azurelib.storage import AzBlob
#
# if not isinstance(self, AzBlob):
name = name.replace(".", "")
self._name = name
def get_name(self):
"""Returns the entity's name"""
return self._name
def get_service(self):
"""Returns the entity's service"""
return self._service
|
ALL_IM_SIZE = (72, 208, 208) # Resize to 72, 256, 256 then crop to 208
CROP = 24
N_CLASSES = 5
ROI_ORDER = ['SpinalCord', 'Lung_R', 'Lung_L', 'Heart', 'Esophagus']
PIXEL_SPACING = 0.975625 # Normalize to this value
INPLANE_SIZE = 512
SLICE_THICKNESS = 3 |
num = int(input('Digite um número: '))
fat = 1
i = 2
while i <= num:
fat = fat * i
i += 1
print(f'O valor de {num}! é = {fat}')
|
def molecule_iterator_filter(
molecule_iterator,
min_fragments=None,
max_fragments=None,
min_mapping_qual=None,
max_mapping_qual=None,
min_ivt_duplicates=None,
max_ivt_duplicates=None,
both_pairs_mapped=None,
min_span=None,
max_span=None
):
""" Filter iterable with molecules
molecule_iterator (iterable) : molecules to filter from
min_fragments (int) : minimum amount of fragments associated to molecule
max_fragments (int) : maximum amount of fragments associated to molecule
min_mapping_qual(int) : minimum maximum mapping quality for a single associated fragment
max_mapping_qual(int) : maximum maximum mapping quality for a single associated fragment
min_ivt_duplicates(int) : minimum amount of in vitro transcription copies
max_ivt_duplicates(int) : maximum amount of in vitro transcription copies
both_pairs_mapped(bool) : molecule should have at least one fragment with both pairs mapped
min_span(int) : minimum amount of bases aligned with reference
max_span : maximum amount of bases aligned with reference
"""
for molecule in molecule_iterator:
if min_fragments is not None and len(molecule)<min_fragments:
continue
if max_fragments is not None and len(molecule)>max_fragments:
continue
if min_mapping_qual is not None and molecule.get_max_mapping_qual()<min_mapping_qual:
continue
if max_mapping_qual is not None and molecule.get_max_mapping_qual()>max_mapping_qual:
continue
if min_ivt_duplicates is not None and len( molecule.get_rt_reactions() ) < min_ivt_duplicates:
continue
if min_ivt_duplicates is not None and len( molecule.get_rt_reactions() ) > max_ivt_duplicates:
continue
if both_pairs_mapped is not None:
found_both = False
for fragment in molecule:
if fragment.has_R1() and fragment.has_R2():
found_both=True
if not found_both:
continue
if min_span is not None and molecule.get_safely_aligned_length()<min_span:
continue
if max_span is not None and molecule.get_safely_aligned_length()>max_span:
continue
yield molecule
|
""" Symlet 12 wavelet """
class Symlet12:
"""
Properties
----------
near symmetric, orthogonal, biorthogonal
All values are from http://wavelets.pybytes.com/wavelet/sym12/
"""
__name__ = "Symlet Wavelet 12"
__motherWaveletLength__ = 24 # length of the mother wavelet
__transformWaveletLength__ = 2 # minimum wavelength of input signal
# decomposition filter
# low-pass
decompositionLowFilter = [
0.00011196719424656033,
-1.1353928041541452e-05,
-0.0013497557555715387,
0.00018021409008538188,
0.007414965517654251,
-0.0014089092443297553,
-0.024220722675013445,
0.0075537806116804775,
0.04917931829966084,
-0.03584883073695439,
-0.022162306170337816,
0.39888597239022,
0.7634790977836572,
0.46274103121927235,
-0.07833262231634322,
-0.17037069723886492,
0.01530174062247884,
0.05780417944550566,
-0.0026043910313322326,
-0.014589836449234145,
0.00030764779631059454,
0.002350297614183465,
-1.8158078862617515e-05,
-0.0001790665869750869,
]
# high-pass
decompositionHighFilter = [
0.0001790665869750869,
-1.8158078862617515e-05,
-0.002350297614183465,
0.00030764779631059454,
0.014589836449234145,
-0.0026043910313322326,
-0.05780417944550566,
0.01530174062247884,
0.17037069723886492,
-0.07833262231634322,
-0.46274103121927235,
0.7634790977836572,
-0.39888597239022,
-0.022162306170337816,
0.03584883073695439,
0.04917931829966084,
-0.0075537806116804775,
-0.024220722675013445,
0.0014089092443297553,
0.007414965517654251,
-0.00018021409008538188,
-0.0013497557555715387,
1.1353928041541452e-05,
0.00011196719424656033,
]
# reconstruction filters
# low pass
reconstructionLowFilter = [
-0.0001790665869750869,
-1.8158078862617515e-05,
0.002350297614183465,
0.00030764779631059454,
-0.014589836449234145,
-0.0026043910313322326,
0.05780417944550566,
0.01530174062247884,
-0.17037069723886492,
-0.07833262231634322,
0.46274103121927235,
0.7634790977836572,
0.39888597239022,
-0.022162306170337816,
-0.03584883073695439,
0.04917931829966084,
0.0075537806116804775,
-0.024220722675013445,
-0.0014089092443297553,
0.007414965517654251,
0.00018021409008538188,
-0.0013497557555715387,
-1.1353928041541452e-05,
0.00011196719424656033,
]
# high-pass
reconstructionHighFilter = [
0.00011196719424656033,
1.1353928041541452e-05,
-0.0013497557555715387,
-0.00018021409008538188,
0.007414965517654251,
0.0014089092443297553,
-0.024220722675013445,
-0.0075537806116804775,
0.04917931829966084,
0.03584883073695439,
-0.022162306170337816,
-0.39888597239022,
0.7634790977836572,
-0.46274103121927235,
-0.07833262231634322,
0.17037069723886492,
0.01530174062247884,
-0.05780417944550566,
-0.0026043910313322326,
0.014589836449234145,
0.00030764779631059454,
-0.002350297614183465,
-1.8158078862617515e-05,
0.0001790665869750869,
]
|
# Crie um programa que tenha uma tupla totalmente preenchida com uma contagem por extenso, de zero até vinte.
# Seu programa deverá ler um número pelo teclado (entre 0 e 20) e mostrá-lo por extenso.
ex = ('Zero','Um','Dois','Tres','Quatro','Cinco',
'Seis','Sete','Oito','Nove','Dez','Onze','Doze'
,'Treze','Quatorze','Quinze','Dezesseis','Dezessete',
'Dezoito','Dezenove','Vinte')
num = int(input('Digite um número entre 0 e 20: '))
while num not in range(0,21):
print('Tente Novamente')
num = int(input('Digite um número entre 0 e 20: '))
print(f'Você Digitou o Número: {ex[num]}')
|
# Complete the rotLeft function below.
def go():
a= [1,2,3]
d = 1
result = rotLeft(a, d)
return result
# print(result, a, d)
def rotLeft(a, d):
# append and pop
value = a.pop(d)
a.append(value)
# print(a)
# return -1
return a
|
# Copyright (c) 2012 The Khronos Group Inc.
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and /or associated documentation files (the "Materials "), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Materials.
# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
class FKeySupplier:
def __init__(self):
self.__returnedKeys = []
self.__maxKey = -1
def __str__(self):
string = "["
for key in self.GetKeyGenerator():
string = string + str(key) + ", "
if (len(string) > 1):
string = string[:-2] # remove the comma and space
string = string + "]"
return string
def NextKey(self):
if (len(self.__returnedKeys) == 0):
self.__maxKey = self.__maxKey + 1
return self.__maxKey
else:
return self.__returnedKeys.pop()
# Assumes you return a key that was given out.
def ReturnKey(self, key):
if (key == self.__maxKey):
self.__maxKey = self.__maxKey - 1
while (self.__returnedKeys.count(self.__maxKey) != 0):
self.__returnedKeys.remove(self.__maxKey)
self.__maxKey = self.__maxKey - 1
else:
self.__returnedKeys.append(key)
def GetKeyGenerator(self):
for key in range(0, self.__maxKey + 1):
if (self.__returnedKeys.count(key) == 0):
yield key
|
__all__ = ['GreetTheWorld']
class GreetTheWorld:
def __init__(self, name: str):
self._name = name
def say_hello(self):
print(f'Hello from GreetTheWorld: {self._name}')
|
VERSION = ('0', '2', '4')
def get_version():
return '.'.join(VERSION)
|
fears = [
"the unknown",
"stepping up",
"going outside",
"getting old",
"being a coward",
"intimacy",
"the public",
"lonliness",
"death",
"losing autonomy",
"seperation",
"mutulation",
"repationships",
"enclosed spaces",
"letting out",
"sharing emotions",
"being judged"
] |
print("--------------------------start--------------------------")
diction={}
d=1
#Function to parse the bracketed problem statement and put the intermediate results(could be terminal or non terminal) into dictionary.
def parse(expression):
def _helper(iter):
items = []
global d
for item in iter:
if item == '(':
result, closeparen = _helper(iter)
if not closeparen:
raise ValueError("Bad expression: Please check if you missed the parentheses")
diction["T"+str(d)] = result;
#print (result)
if 4 > len(result) > 0 :
result="T"+str(d)
d=d+1
items.append(result)
#print (result)
elif item == ')':
return items, True
else:
items.append(item)
return items, False
return _helper(iter(expression))[0]
problem_stmt="-(((p)&(-(q)))|((p)|(r)))"
#Please uncomment one of the following problem and comment above for checking the output.
#Also don't give any spaces or If you want we can write trimming code for handling this condition
#problem_stmt="((p)|(-(q)))"
#problem_stmt="((p)|(q))"
#problem_stmt="-((p)&(q))"
#problem_stmt="(((p)|(q))|(r))"
#problem_stmt="(((p)&(q))|((p)&(r)))|((q)&(r))"
#problem_stmt="(((p)&((q)|(r)))|((q)|(r)))"
problem = parse(problem_stmt)
#adding last/root element
diction["T"+str(d)]=problem;
#print 'Debugging point#1 : '+str(d)
# now performing flipping of operators over the and/or over the 3 length elements
final_dict={}
j=0;
for i in range(len(diction)):
#print 'Debugging point#2 : '+str(i+1) +' = ' + ' '.join(diction["T"+str(i+1)])
j=j+1
#if len=3 that means involvement of binary operator
if len(diction["T"+str(i+1)])==3 :
#print 'Debugging point#3 : '+''.join(diction[diction["T"+str(i+1)][2]])
# used join in following statement because a single element of dictionary is treated as list
a=''.join(diction[diction["T"+str(i+1)][0]])
b=''.join(diction[diction["T"+str(i+1)][2]])
#resetting the non-terminal symbols with terminal symbols
if len(a)>1:
diction["T"+str(i+1)][0]= '('+a+')'
else :
diction["T"+str(i+1)][0]= a
if len(b)>1:
diction["T"+str(i+1)][2]= '('+b+')'
else :
diction["T"+str(i+1)][2]= b
final_dict[j]= ''.join(diction["T"+str(i+1)][0]) +'&'+''.join(diction["T"+str(i+1)][2])
j=j+1
final_dict[j]= ''.join(diction["T"+str(i+1)][0]) +'|'+''.join(diction["T"+str(i+1)][2])
#if len=2 that means involvement of unary operator
elif len(diction["T"+str(i+1)])==2 :
#since here a would be an operator
a=''.join(diction["T"+str(i+1)][0])
#since here b would be a non terminal symbol
b=''.join(diction[diction["T"+str(i+1)][1]])
#resetting the non-terminal symbols with terminal symbols
diction["T"+str(i+1)][1]= '('+b+')'
final_dict[j]=a+'('+b+')'
else :
if len(diction) !=i+1:
final_dict[j]= ''.join(diction["T"+str(i+1)])
#print str(j)+'Debugging point#4 : '+''.join(final_dict[j])
print ("Your problem statement : " + problem_stmt)
print ("Your plausible ALT solutions are as followed : ")
for k in range(len(final_dict)):
print (str(k+1) +' = ' + ''.join(final_dict[k+1]))
print ("--------------------------end--------------------------") |
# implementation of the trie data structure
class TrieNode:
def __init__(self):
self.children = {}
self.char = '*'
self.value = None
self.end = False
def put(self, key, value):
if len(key) == 0:
print("Error: Empty Key")
return False
head = key[0]
if head in self.children:
current_node = self.children[head]
else:
current_node = TrieNode()
current_node.char = head
self.children[head] = current_node
if len(key) > 1:
tail = key[1:]
current_node.put(tail, value)
else:
current_node.value = value
current_node.end = True
return True
def get(self, key):
if len(key) == 0:
print("Error: Empty Key")
return None
head = key[0]
if head in self.children:
current_node = self.children[head]
else:
return None
val = None
if len(key) > 1:
tail = key[1:]
val = current_node.get(tail)
elif current_node.end:
val = current_node.value
return val
def query(self, top_lvl_key, keys):
if len(top_lvl_key) == 0:
print('Error: empty top level key')
return None
val = self.get(top_lvl_key)
if val is None:
return None
if len(keys) == 0:
return val
else:
for key in keys:
if len(key) == 0:
print('Error: query secondary key path contains empty key')
return None
try:
val = val.get(key)
except (AttributeError, TypeError):
return None
return val
def delete(self, key):
if len(key) == 0:
print("Error: Empty Key")
return None
head = key[0]
if head in self.children:
current_node = self.children[head]
else:
return None
val = None
if len(key) > 1:
tail = key[1:]
val = current_node.delete(tail)
elif current_node.end:
current_node.end = False
val = current_node.value
current_node.value = None
return val
|
if __name__ == '__main__':
N, M = map(int, input().split())
for i in range(1, N, 2):
print((i * ".|.").center(M, "-"))
print("welcome".upper().center(M, "-"))
for i in range(N-2, -1, -2):
print((i * ".|.").center(M, "-"))
|
# -*- coding:utf-8 -*-
# https://leetcode.com/problems/restore-ip-addresses/description/
class Solution(object):
def restoreIpAddresses(self, s):
"""
:type s: str
:rtype: List[str]
"""
ret = []
def f(s, address):
if not s or len(address) == 4:
not s and len(address) == 4 and ret.append(''.join(address))
return
bg, ed = 1, 1 if s[0] == '0' else min(3, len(s))
for i in xrange(bg, ed + 1):
digit = s[:i]
if 0 <= int(digit) <= 255:
if len(address) < 3:
address.append(digit + '.')
else:
address.append(digit)
f(s[i:], address)
address.pop()
f(s, [])
return ret |
print("Welcome to the tip calculator.")
bill = float(input("What was the total bill? $"))
tip = int(input("What percentage tip would you like to give? 10, 12, or 15? "))
total_people = int(input("How many people to split the bill? "))
total_tip = (bill * (tip / 100))
total_bill = (bill + total_tip)
payment = (total_bill / total_people)
print(f"Each person should pay: ${payment:.2f}") |
class LyricsNotFound(Exception):
pass
class ElasticSearchConnectionError(Exception):
pass
class InvalidRepository(Exception):
pass
class ArtistNotFound(Exception):
pass
class ConfigError(Exception):
pass
|
# Create an infinite loop and break it when the user enters the word 'stop'.
while True:
# Ask the user for a word.
word = input("Enter an word: ")
# Stop when the user enters the word 'stop'.
if word == "stop":
break
# Print the word back to the user followed by its length.
print(word, len(word))
# Similarly to Task1 instead of an infinite while loop we could have used a finite while loop with the expression
# word != 'stop', like this: while word != 'stop'.
|
def math():
n = int(input())
for i in range(n):
if i == n-1:
print('Ho', end='')
else:
print('Ho', end=' ')
print('!')
if __name__ == '__main__':
math()
|
load(":actions.bzl", "nim_compile")
def _nim_binary_impl(ctx):
# Declare an output file for the main package and compile it from srcs.
executable = ctx.actions.declare_file(ctx.label.name)
nim_exe= ctx.executable._nim
nim_compile(
ctx,
nim_exe = nim_exe,
projectfile = ctx.file.projectfile,
srcs = ctx.files.srcs,
out = executable,
)
# Return the DefaultInfo provider. This tells Bazel what files should be
# built when someone asks to build a nim_binary rule. It also says which
# one is executable (in this case, there's only one).
return [
DefaultInfo(
files = depset([executable]),
executable = executable,
)
]
nim_binary = rule(
_nim_binary_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".nim"],
doc = "Source files to compile for this package",
),
"projectfile": attr.label(
allow_single_file = True,
doc = "Nim 'projectfile' to compile for this package",
mandatory = True,
),
"_nim": attr.label(
allow_single_file = True,
default = "@nim_prebuilt//:exe",
executable = True,
cfg = "exec",
)
},
doc = "Builds an executable program from Nim-lang source code",
executable = True,
) |
#!/usr/bin/env python3
sumsq = sum([i**2 for i in range(1, 101)])
sqsum = sum(range(1, 101))**2
print(sqsum-sumsq)
|
class HostOperationSystemVersion(object):
def read_get(self, name, idx_name, unity_client):
return unity_client.get_host_os_type(idx_name)
class HostOperationSystemVersionColumn(object):
def get_idx(self, name, idx, unity_client):
return unity_client.get_hosts()
|
k=int(input())
m=int(input())
list1=[int(i) for i in range(1,k+1)]
for i in range(m):
action=int(input())
list2=[]
for e in range(0,len(list1)+1-action,action):
if e==0:
pass
else:
list2.append(e)
for c in list2:
list1.remove(c)
list1.sort()
for i in list1:
print(i) |
"""Tests the routes for the Errors module."""
def test_404(app, client):
"""Check if the application can route to a 404."""
with app.app_context():
response = client.get('/unknown_page')
assert response.status_code == 404
assert 'Page Not Found' in str(response.data)
def test_500(app, client):
"""Check if the application can route to a 500."""
with app.app_context():
response = client.get('/err500')
assert response.status_code == 500
assert 'An Error Has Occurred' in str(response.data)
|
class GenericTreeNode:
def __init__(self, data=None, firstChild=None, nextSibling=None):
self.data = data
self.firstChild = firstChild
self.nextSibling = nextSibling
def initializeGenericTree() -> GenericTreeNode:
c = GenericTreeNode(4)
e = GenericTreeNode(5)
a = GenericTreeNode(3, firstChild=c, nextSibling=e)
b = GenericTreeNode(2, nextSibling=a)
d = GenericTreeNode(1, firstChild=b)
return d
def initializeGenericTreeForSiblings() -> GenericTreeNode:
c = GenericTreeNode(4)
d = GenericTreeNode(5)
a = GenericTreeNode(3, firstChild=c, nextSibling=d)
b = GenericTreeNode(2, firstChild=a)
return b
|
'''
Descripttion:
version:
Author: HuSharp
Date: 2021-02-22 12:12:48
LastEditors: HuSharp
LastEditTime: 2021-02-23 23:16:08
@Email: 8211180515@csu.edu.cn
'''
class A:
z = -1
def f(self, x):
return B(x-1)
class B(A):
n = 4
def __init__(self, y):
if y:
self.z = self.f(y)
else:
self.z = C(y+1)
class C(B):
def f(self, x):
return x |
# coding: utf-8
#
# Copyright 2022 :Barry-Thomas-Paul: Moss
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http: // www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Const Class
# this is a auto generated file generated by Cheetah
# Libre Office Version: 7.3
# Namespace: com.sun.star.i18n
class LocaleItem(object):
"""
Const Class
These are not used with the API but with an OOo internal wrapper class that caches the contents of an instance of LocaleDataItem and uses these values to access it's members for faster access.
Whenever locale data items were added these values and the wrapper class would have to be adjusted to give the application an easier access.
.. deprecated::
Class is deprecated.
See Also:
`API LocaleItem <https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1i18n_1_1LocaleItem.html>`_
"""
__ooo_ns__: str = 'com.sun.star.i18n'
__ooo_full_ns__: str = 'com.sun.star.i18n.LocaleItem'
__ooo_type_name__: str = 'const'
DATE_SEPARATOR = 0
THOUSAND_SEPARATOR = 1
DECIMAL_SEPARATOR = 2
TIME_SEPARATOR = 3
TIME_100SEC_SEPARATOR = 4
LIST_SEPARATOR = 5
SINGLE_QUOTATION_START = 6
SINGLE_QUOTATION_END = 7
DOUBLE_QUOTATION_START = 8
DOUBLE_QUOTATION_END = 9
MEASUREMENT_SYSTEM = 10
TIME_AM = 11
TIME_PM = 12
LONG_DATE_DAY_OF_WEEK_SEPARATOR = 13
LONG_DATE_DAY_SEPARATOR = 14
LONG_DATE_MONTH_SEPARATOR = 15
LONG_DATE_YEAR_SEPARATOR = 16
COUNT = 17
"""
count of items available
"""
DECIMAL_SEPARATOR_ALTERNATIVE = 17
COUNT2 = 18
"""
count of items available
"""
__all__ = ['LocaleItem']
|
def print_full_name(a, b):
b=b+'!'
print("Hello",a,b, "You just delved into python.")
|
"""
基准条件
递归条件
"""
def quicksort(src_list):
if len(src_list) == 0:
return []
if len(src_list) ==1:
return [src_list[0]]
pivot=src_list[0]
left = [val for val in src_list[1:] if val<pivot]
right = [val for val in src_list[1:] if val > pivot]
return quicksort(left)+ [pivot] + quicksort(right)
if __name__ == '__main__':
t=quicksort([8,4,3,2,12,54,9])
print(t) |
class Element:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self, head=None):
self.head = head
def append(self, new_element):
current = self.head
if self.head:
while current.next:
current = current.next
current.next = new_element
else:
self.head = new_element
def get_position(self, position):
current_element = self.head
try:
for _ in range(1, position):
current_element = current_element.next
return current_element
except:
return None
def insert_first(self, new_element):
current = self.head
self.head = new_element
new_element.next = current
def delete_first(self):
try:
poped_item = self.head
self.head = self.head.next
return poped_item
except:
return None
class Stack(LinkedList):
def __init__(self, top=None):
self.linkedlist = LinkedList(top)
def push(self, new_element):
self.linkedlist.insert_first(new_element)
def pop(self):
return self.linkedlist.delete_first()
|
"""
Vol I Lab __: GMRES
Name:
Date:
"""
#Problem 1: Implement the following function
def gmres(A, b, x0, k=100, tol=1e-8):
'''Calculate approximate solution of Ax=b using GMRES algorithm.
INPUTS:
A - Callable function that calculates Ax for any input vector x.
b - A NumPy array of length m.
x0 - An arbitrary initial guess.
k - Maximum number of iterations of the GMRES algorithm. Defaults to 100.
tol - Stop iterating if the residual is less than 'tol'. Defaults to 1e-8.
RETURN:
Return (y, res) where 'y' is an approximate solution to Ax=b and 'res'
is the residual.
'''
raise NotImplementedError("Problem 1 incomplete.")
#Problem 2: Implement the following two functions
def plot_gmres(A, b, x0, tol=1e-8):
'''Use the GMRES algorithm to approximate the solution to Ax=b. Plot the
eigenvalues of A and the convergence of the algorithm.
INPUTS:
A - A 2-D NumPy array of shape mxm.
b - A 1-D NumPy array of length m.
x0 - An arbitrary initial guess.
tol - Stop iterating and create the desired plots when the residual is
less than 'tol'. Defaults to 1e-8.
OUTPUT:
Follow the GMRES algorithm until the residual is less than tol, for a
maximum of m iterations. Then create the two following plots (subplots
of a single figure):
1. Plot the eigenvalues of A in the complex plane.
2. Plot the convergence of the GMRES algorithm by plotting the
iteration number on the x-axis and the residual on the y-axis.
Use a log scale on the y-axis.
'''
raise NotImplementedError("Problem 2 incomplete.")
def make_plots(m=200):
'''Create the matrix An defined in problem 2 in the manual
for n = -4, -2, -0, 2, 4. Call plot_gmres on each, with b
a vector of ones, and an initial guess x0 a vector of zeros.
Print a statement explaining how the convergence relates to
the eigenvalues.
'''
raise NotImplementedError("make_plots not yet implemented.")
#Problem 3: Implement the following two functions
def gmres_k(Amul, b, x0, k=5, tol=1E-8, restarts=50):
'''Use the GMRES(k) algorithm to approximate the solution to Ax=b.
INPUTS:
A - A Callable function that calculates Ax for any vector x.
b - A NumPy array.
x0 - An arbitrary initial guess.
k - Maximum number of iterations of the GMRES algorithm before
restarting. Defaults to 100.
tol - Stop iterating if the residual is less than 'tol'. Defaults
to 1E-8.
restarts - Maximum number of restarts. Defaults to 50.
OUTPUT:
Return (y, res) where 'y' is an approximate solution to Ax=b and 'res'
is the residual.
'''
raise NotImplementedError("Problem 3 incomplete.")
def time_gmres(m=200):
'''Time the gmres and gmres_k functions on each of the matrices
from problem 2. Let x0 be a vector of zeros or anything you like.
The results might be more dramatic with an x0 of larger magnitude.
Print your results. What do you observe?
'''
raise NotImplementedError("time_gmres not yet implemented.")
|
"""
This class Environment sets up the universe for the robot.
"""
class Environment:
"""Environment class
"""
def __init__(self):
self._detected_obj: [CoherentItem] = None
|
def c12_config():
return {
"data_table": "default",
"diag_table": "default",
"experiment_name": "default",
"forcing": "gs://vcm-fv3config/data/base_forcing/v1.1/",
"orographic_forcing": "gs://vcm-fv3config/data/orographic_data/v1.0",
"initial_conditions": "gs://vcm-fv3config/data/initial_conditions/gfs_c12_example/v1.0",
"namelist": {
"amip_interp_nml": {
"data_set": "reynolds_oi",
"date_out_of_range": "climo",
"interp_oi_sst": True,
"no_anom_sst": False,
"use_ncep_ice": False,
"use_ncep_sst": True,
},
"atmos_model_nml": {
"blocksize": 24,
"chksum_debug": False,
"dycore_only": False,
"fdiag": 0.0,
"fhmax": 1024.0,
"fhmaxhf": -1.0,
"fhout": 0.25,
"fhouthf": 0.0,
},
"cires_ugwp_nml": {
"knob_ugwp_azdir": [2, 4, 4, 4],
"knob_ugwp_doaxyz": 1,
"knob_ugwp_doheat": 1,
"knob_ugwp_dokdis": 0,
"knob_ugwp_effac": [1, 1, 1, 1],
"knob_ugwp_ndx4lh": 4,
"knob_ugwp_solver": 2,
"knob_ugwp_source": [1, 1, 1, 0],
"knob_ugwp_stoch": [0, 0, 0, 0],
"knob_ugwp_version": 0,
"knob_ugwp_wvspec": [1, 32, 32, 32],
"launch_level": 55,
},
"coupler_nml": {
"atmos_nthreads": 1,
"calendar": "julian",
"current_date": [2016, 8, 1, 0, 0, 0],
"days": 0,
"dt_atmos": 900,
"dt_ocean": 900,
"hours": 0,
"memuse_verbose": True,
"minutes": 30,
"months": 0,
"ncores_per_node": 32,
"seconds": 0,
"use_hyper_thread": True,
},
"diag_manager_nml": {"prepend_date": False},
"external_ic_nml": {
"checker_tr": False,
"filtered_terrain": True,
"gfs_dwinds": True,
"levp": 64,
"nt_checker": 0,
},
"fms_io_nml": {
"checksum_required": False,
"max_files_r": 100,
"max_files_w": 100,
},
"fms_nml": {
"clock_grain": "ROUTINE",
"domains_stack_size": 3000000,
"print_memory_usage": False,
},
"fv_core_nml": {
"a_imp": 1.0,
"adjust_dry_mass": False,
"beta": 0.0,
"consv_am": False,
"consv_te": 1.0,
"d2_bg": 0.0,
"d2_bg_k1": 0.16,
"d2_bg_k2": 0.02,
"d4_bg": 0.15,
"d_con": 1.0,
"d_ext": 0.0,
"dddmp": 0.2,
"delt_max": 0.002,
"dnats": 1,
"do_sat_adj": True,
"do_vort_damp": True,
"dwind_2d": False,
"external_ic": True,
"fill": True,
"fv_debug": False,
"fv_sg_adj": 900,
"gfs_phil": False,
"hord_dp": 6,
"hord_mt": 6,
"hord_tm": 6,
"hord_tr": 8,
"hord_vt": 6,
"hydrostatic": False,
"io_layout": [1, 1],
"k_split": 1,
"ke_bg": 0.0,
"kord_mt": 10,
"kord_tm": -10,
"kord_tr": 10,
"kord_wz": 10,
"layout": [1, 1],
"make_nh": True,
"mountain": False,
"n_split": 6,
"n_sponge": 4,
"na_init": 1,
"ncep_ic": False,
"nggps_ic": True,
"no_dycore": False,
"nord": 2,
"npx": 13,
"npy": 13,
"npz": 63,
"ntiles": 6,
"nudge": False,
"nudge_qv": True,
"nwat": 6,
"p_fac": 0.1,
"phys_hydrostatic": False,
"print_freq": 3,
"range_warn": True,
"reset_eta": False,
"rf_cutoff": 800.0,
"rf_fast": False,
"tau": 5.0,
"use_hydro_pressure": False,
"vtdm4": 0.06,
"warm_start": False,
"z_tracer": True,
},
"fv_grid_nml": {},
"gfdl_cloud_microphysics_nml": {
"c_cracw": 0.8,
"c_paut": 0.5,
"c_pgacs": 0.01,
"c_psaci": 0.05,
"ccn_l": 300.0,
"ccn_o": 100.0,
"const_vg": False,
"const_vi": False,
"const_vr": False,
"const_vs": False,
"de_ice": False,
"do_qa": True,
"do_sedi_heat": False,
"dw_land": 0.16,
"dw_ocean": 0.1,
"fast_sat_adj": True,
"fix_negative": True,
"icloud_f": 1,
"mono_prof": True,
"mp_time": 450.0,
"prog_ccn": False,
"qi0_crt": 8e-05,
"qi_lim": 1.0,
"ql_gen": 0.001,
"ql_mlt": 0.001,
"qs0_crt": 0.001,
"rad_graupel": True,
"rad_rain": True,
"rad_snow": True,
"rh_inc": 0.3,
"rh_inr": 0.3,
"rh_ins": 0.3,
"rthresh": 1e-05,
"sedi_transport": False,
"tau_g2v": 900.0,
"tau_i2s": 1000.0,
"tau_l2v": [225.0],
"tau_v2l": 150.0,
"use_ccn": True,
"use_ppm": False,
"vg_max": 12.0,
"vi_max": 1.0,
"vr_max": 12.0,
"vs_max": 2.0,
"z_slope_ice": True,
"z_slope_liq": True,
},
"gfs_physics_nml": {
"cal_pre": False,
"cdmbgwd": [3.5, 0.25],
"cnvcld": False,
"cnvgwd": True,
"debug": False,
"dspheat": True,
"fhcyc": 24.0,
"fhlwr": 3600.0,
"fhswr": 3600.0,
"fhzero": 0.25,
"hybedmf": True,
"iaer": 111,
"ialb": 1,
"ico2": 2,
"iems": 1,
"imfdeepcnv": 2,
"imfshalcnv": 2,
"imp_physics": 11,
"isol": 2,
"isot": 1,
"isubc_lw": 2,
"isubc_sw": 2,
"ivegsrc": 1,
"ldiag3d": False,
"lwhtr": True,
"ncld": 5,
"nst_anl": True,
"pdfcld": False,
"pre_rad": False,
"prslrd0": 0.0,
"random_clds": False,
"redrag": True,
"shal_cnv": True,
"swhtr": True,
"trans_trac": True,
"use_ufo": True,
},
"interpolator_nml": {"interp_method": "conserve_great_circle"},
"nam_stochy": {"lat_s": 96, "lon_s": 192, "ntrunc": 94},
"namsfc": {
"fabsl": 99999,
"faisl": 99999,
"faiss": 99999,
"fnabsc": "grb/global_mxsnoalb.uariz.t1534.3072.1536.rg.grb",
"fnacna": "",
"fnaisc": "grb/CFSR.SEAICE.1982.2012.monthly.clim.grb",
"fnalbc": "grb/global_snowfree_albedo.bosu.t1534.3072.1536.rg.grb",
"fnalbc2": "grb/global_albedo4.1x1.grb",
"fnglac": "grb/global_glacier.2x2.grb",
"fnmskh": "grb/seaice_newland.grb",
"fnmxic": "grb/global_maxice.2x2.grb",
"fnslpc": "grb/global_slope.1x1.grb",
"fnsmcc": "grb/global_soilmgldas.t1534.3072.1536.grb",
"fnsnoa": "",
"fnsnoc": "grb/global_snoclim.1.875.grb",
"fnsotc": "grb/global_soiltype.statsgo.t1534.3072.1536.rg.grb",
"fntg3c": "grb/global_tg3clim.2.6x1.5.grb",
"fntsfa": "",
"fntsfc": "grb/RTGSST.1982.2012.monthly.clim.grb",
"fnvegc": "grb/global_vegfrac.0.144.decpercent.grb",
"fnvetc": "grb/global_vegtype.igbp.t1534.3072.1536.rg.grb",
"fnvmnc": "grb/global_shdmin.0.144x0.144.grb",
"fnvmxc": "grb/global_shdmax.0.144x0.144.grb",
"fnzorc": "igbp",
"fsicl": 99999,
"fsics": 99999,
"fslpl": 99999,
"fsmcl": [99999, 99999, 99999],
"fsnol": 99999,
"fsnos": 99999,
"fsotl": 99999,
"ftsfl": 99999,
"ftsfs": 90,
"fvetl": 99999,
"fvmnl": 99999,
"fvmxl": 99999,
"ldebug": False,
},
},
}
|
class ValidatorVector:
value = None
def __get__(self, obj, objtype=None):
return self.value
def __set__(self, obj, value):
if value is not None:
if not isinstance(value, list):
raise TypeError(f'Expected {value!r} to be an list')
if obj.n is not None:
if len(value) != obj.n:
raise TypeError("Vector should be of length " + str(obj.n))
self.value = value
|
'''
Description:
Author: HCQ
Company(School): UCAS
Email: 1756260160@qq.com
Date: 2021-09-12 11:16:43
LastEditTime: 2021-10-26 14:55:11
FilePath: /mmdetection3d/configs/_base_/models/hv_pointpillars_secfpn_ouster.py
'''
# voxel_size = [0.16, 0.16, 4] # 长宽高
voxel_size = [0.25, 0.25, 10.5] # 长宽高 z高度要和point_cloud_range高度一致z:[-3, 10]
# RuntimeError: cannot perform reduction function max on tensor with no elements because the operation does not have an identity
model = dict(
type='VoxelNet', # voxelnet.py模型名字 mmdet3d/models/detectors/__init__.py mmdet3d/models/detectors/voxelnet.py
voxel_layer=dict(
max_num_points=32, # max_points_per_voxel
# point_cloud_range=[0, -39.68, -3, 69.12, 39.68, 1], # [0, -40, -3, 60, 40, 7.5] 上层已修改
point_cloud_range = [-30, -40, -3, 60, 40, 7.5],
voxel_size=voxel_size,
max_voxels=(16000, 40000) # (training, testing) max_voxels
),
# 'pillar_features'torch.Size([17842, 64]) # pillar特征(C, P)的Tensor,特征维度C=64,Pillar非空P=17842个
voxel_encoder=dict(
type='PillarFeatureNet', # init--> from .pillar_encoder import PillarFeatureNet --> mmdet3d/models/voxel_encoders/pillar_encoder.py
in_channels=4,
feat_channels=[64], # pillar特征(C, P)的Tensor,特征维度C=64,Pillar非空P=17842个
with_distance=False,
voxel_size=voxel_size,
# point_cloud_range=[0, -39.68, -3, 69.12, 39.68, 1]),# 上层已修改
point_cloud_range = [-30, -40, -3, 60, 40, 7.5]
),
middle_encoder=dict(
type='PointPillarsScatter', in_channels=64, output_shape=[496, 432]), # 生成伪造图像,图像维度为(1,64,496,432) mmdet3d/models/middle_encoders/pillar_scatter.py
backbone=dict( # 调用注册器的backbone
type='SECOND', # backbone名字
# conv_cfg=dict(type='DCN', bias=False) #
in_channels=64, #
layer_nums=[3, 5, 5],
layer_strides=[2, 2, 2],
out_channels=[64, 128, 256]), # [64, 128, 256]
neck=dict(
type='SECONDFPN',
in_channels=[64, 128, 256],
upsample_strides=[1, 2, 4],
out_channels=[128, 128, 128]),
bbox_head=dict(
type='Anchor3DHead',
num_classes=7, # 修改为7类=============================
in_channels=384,
feat_channels=384,
use_direction_classifier=True,
anchor_generator=dict( # 生成anchor
type='Anchor3DRangeGenerator', # https://mmdetection3d.readthedocs.io/zh_CN/latest/api.html#mmdet3d.core.anchor.AlignedAnchor3DRangeGenerator
# ranges=[ # ???????????修改生成anchor的总范围=============================================================================
# [0, -39.68, -0.6, 70.4, 39.68, -0.6], # (x_min, y_min, z_min, x_max, y_max, z_max).
# [0, -39.68, -0.6, 70.4, 39.68, -0.6],
# [0, -39.68, -1.78, 70.4, 39.68, -1.78],
# ],
ranges=[ # 修改生成anchor的总范围=============================================================================
[-30, -40, -0.37, 60, 40, -0.37], # (x_min, y_min, z_min, x_max, y_max, z_max).
[-30, -40, -0.47, 60, 40, -0.47],
[-30, -40, -3.44, 60, 40, -3.44], # 可参考configs/_base_/models/hv_pointpillars_fpn_nus.py
],
# sizes=[[0.6, 0.8, 1.73], [0.6, 1.76, 1.73], [1.6, 3.9, 1.56]], # ['Pedestrian', 'Cyclist', 'Car'] Anchor size with shape [N, 3], in order of x, y, z.
# sizes=[[6.5, 13, 6.5], [3.5, 7, 3], [3.9, 1.6, 1.56]],
sizes=[[6.5, 13, 6.5], [3.5, 7, 3], [3.9, 1.6, 1.56]], # 3D sizes of anchors. class_names = 'Truck','Auxiliary','Car','Excavator','Widebody','Pedestrian','Others' ('Truck','Car','Pedestrian','Excavator','Widebody','Auxiliary','Others')
rotations=[0, 1.57],
reshape_out=False),
diff_rad_by_sin=True,
# bbox_coder=dict(type='DeltaXYZWLHRBBoxCoder'),
bbox_coder=dict(type='DeltaXYZWLHRBBoxCoderOuster'), # 修改为DeltaXYZWLHRBBoxCoderOuster mmdet3d/core/bbox/coders/delta_xyzwhlr_bbox_coder_ouster.py============================
# 分类loss,回归loss,朝向loss
loss_cls=dict(
type='FocalLoss',
use_sigmoid=True,
gamma=2.0,
alpha=0.25,
loss_weight=1.0),
loss_bbox=dict(type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=2.0),
loss_dir=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.2)),
# model training and testing settings
train_cfg=dict(
assigner=[ # 'Truck','Auxiliary','Car'
dict( # for Pedestrian
type='MaxIoUAssigner',
iou_calculator=dict(type='BboxOverlapsNearest3D'),
pos_iou_thr=0.5,
neg_iou_thr=0.35,
min_pos_iou=0.35,
ignore_iof_thr=-1),
dict( # for Cyclist
type='MaxIoUAssigner',
iou_calculator=dict(type='BboxOverlapsNearest3D'),
pos_iou_thr=0.5,
neg_iou_thr=0.35,
min_pos_iou=0.35,
ignore_iof_thr=-1),
# dict( # for Car
# type='MaxIoUAssigner',
# iou_calculator=dict(type='BboxOverlapsNearest3D'),
# pos_iou_thr=0.6,
# neg_iou_thr=0.45,
# min_pos_iou=0.45,
# ignore_iof_thr=-1),
dict( # for Car
type='MaxIoUAssigner',
iou_calculator=dict(type='BboxOverlapsNearest3D'),
pos_iou_thr=0.45,
neg_iou_thr=0.25,
min_pos_iou=0.25,
ignore_iof_thr=-1),
],
allowed_border=0,
pos_weight=-1,
debug=False),
test_cfg=dict(
use_rotate_nms=True,
nms_across_levels=False,
nms_thr=0.01, # 去重 nms_thr=0.01,
score_thr=0.0001, # 修改阈值 score_thr=0.1,
min_bbox_size=0,
nms_pre=100,
max_num=5) # max_num
)
|
#
# Collective Knowledge (dealing with table)
#
# See CK LICENSE.txt for licensing details
# See CK COPYRIGHT.txt for copyright details
#
# Developer: Grigori Fursin, Grigori.Fursin@cTuning.org, http://fursin.net
#
cfg={} # Will be updated by CK (meta description of this module)
work={} # Will be updated by CK (temporal data)
ck=None # Will be updated by CK (initialized CK kernel)
# Local settings
##############################################################################
# Initialize module
def init(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
return {'return':0}
##############################################################################
# draw table
def draw(i):
"""
Input: {
table - table to draw [[],[],[]...], [[],[],[]...] ...]
(out) - txt (default) or html
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
string - output
}
"""
o=i.get('out','')
table=i.get('table',[])
s=''
if len(table)>0:
lx=len(table[0])
lwidth=[]
for l in range(0, lx):
lwidth.append(-1)
# If 'txt', check length of all entries
if o=='txt':
for t in table:
for l in range(0, lx):
sx=str(t[l])
lw=lwidth[l]
if lw==-1 or len(sx)>lw:
lwidth[l]=len(sx)
for t in table:
for l in range(0, lx):
sx=str(t[l])
lw=lwidth[l]
s+=sx.ljust(lw+2)
s+='\n'
else:
s='<html>\n'
s+=' <body>\n'
s+=' <table border="1">\n'
for t in table:
s+=' <tr>\n'
for l in range(0, lx):
sx=str(t[l])
s+=' <td>'+sx+'</td>\n'
s+=' </tr>\n'
s+=' </table>\n'
s+=' </body>\n'
s+='<html>\n'
return {'return':0, 'string':s}
##############################################################################
# prepare table (in HTML and LaTex)
def prepare(i):
"""
Input: {
table
table_header
(table_custom)
(table_style)
(header_style)
(header_element_style)
(element_style)
(row_style)
(html_before_table)
(html_after_table)
(tex_before_table)
(tex_after_table)
(record_html) - file (with path) to record produced HTML
(record_tex) - file (with path) to record produced TEX
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
html - prepared HTML
tex - prepared :aTex
}
"""
table=i['table']
table_header=i['table_header']
table_custom=i.get('table_custom',{})
h=i.get('html_before_table','') # HTML
t=i.get('tex_before_table','') # LaTex
ts=i.get('table_style','')
hs=i.get('header_style','')
rs=i.get('row_style','')
hes=i.get('header_element_style','')
es=i.get('element_style','')
# Preparing Header
h+='<table '+ts+'>\n'
t+=' \\begin{tabular}{|'
for x in table_header:
t+=x.get('tex','')+'|'
t+='}\n'
h+=' <tr '+hs+'>\n'
t+=' \\hline\n'
t+=' '
first=True
for x in table_header:
n=x.get('name','')
if first:
first=False
else:
t+=' & '
t+='\\textbf{'+n+'}'
if x.get('html_change_space','')=='yes':
n=n.replace(' ',' ')
h+=' <td '+hes+'>\n'
h+=' <b>'+n+'</b>\n'
h+=' </td>\n'
h+=' </tr>\n'
t+=' \\\\ \n'
# Preparing table
for ix in range(0, len(table)):
x=table[ix]
cx={}
if ix<len(table_custom):
cx=table_custom[ix]
rs1=cx.get('row_style','')
h+=' <tr '+rs+' '+rs1+'>\n'
t+=' \\hline\n'
t+=' '
first=True
for iy in range(0, len(x)):
st={}
if iy<len(table_header):
st=table_header[iy]
y=cx.get('field_'+str(iy)+'_html','')
if y=='':
y=str(x[iy])
if st.get('html_change_space','')=='yes':
y=y.replace(' ',' ')
y=y.replace('\\newline',' ')
h+=' <td '+es+'>\n'
if st.get('html_before','')!='':
h+=' '+st['html_before']
h+=' '+str(y)+'\n'
if st.get('html_after','')!='':
h+=' '+st['html_after']
h+=' </td>\n'
z=cx.get('field_'+str(iy)+'_tex','')
if z=='':
z=str(x[iy])
if first:
first=False
else:
t+=' & '
if st.get('tex_before','')!='':
t+=st['tex_before']
t+=' '+str(z)+' '
if st.get('tex_after','')!='':
t+=st['tex_after']
t+='\\\\\n'
h+=' </tr>\n'
t+=" \\hline\n"
# Finalizing
h+='</table>\n'
h+=i.get('html_after_table','')
t+=' \\end{tabular}'
t+=' '+i.get('tex_after_table','')
# Check if record
if i.get('record_html','')!='':
r=ck.save_text_file({'text_file':i['record_html'], 'string':h})
if i.get('record_tex','')!='':
r=ck.save_text_file({'text_file':i['record_tex'], 'string':t})
return {'return':0, 'html':h, 'tex':t}
|
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
# and others. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class Project:
"""
SNAPS domain class for Projects. Should contain attributes that
are shared amongst cloud providers
"""
def __init__(self, name, project_id, domain_id=None):
"""
Constructor
:param name: the project's name
:param project_id: the project's id
:param domain_id: the project's domain id
"""
self.name = name
self.id = project_id
self.domain_id = domain_id
def __eq__(self, other):
return self.name == other.name and self.id == other.id
class Domain:
"""
SNAPS domain class for OpenStack Keystone v3+ domains.
"""
def __init__(self, name, domain_id=None):
"""
Constructor
:param name: the project's name
:param domain_id: the project's domain id
"""
self.name = name
self.id = domain_id
def __eq__(self, other):
return self.name == other.name and self.id == other.id
class ComputeQuotas:
"""
SNAPS domain class for holding project quotas for compute services
"""
def __init__(self, nova_quotas=None, **kwargs):
"""
Constructor
:param nova_quotas: the OS nova quota object
"""
if nova_quotas:
self.metadata_items = nova_quotas.metadata_items
self.cores = nova_quotas.cores # aka. VCPUs
self.instances = nova_quotas.instances
self.injected_files = nova_quotas.injected_files
self.injected_file_content_bytes = nova_quotas.injected_file_content_bytes
self.ram = nova_quotas.ram
self.fixed_ips = nova_quotas.fixed_ips
self.key_pairs = nova_quotas.key_pairs
else:
self.metadata_items = kwargs.get('metadata_items')
self.cores = kwargs.get('cores') # aka. VCPUs
self.instances = kwargs.get('instances')
self.injected_files = kwargs.get('injected_files')
self.injected_file_content_bytes = kwargs.get(
'injected_file_content_bytes')
self.ram = kwargs.get('ram')
self.fixed_ips = kwargs.get('fixed_ips')
self.key_pairs = kwargs.get('key_pairs')
def __eq__(self, other):
return (self.metadata_items == other.metadata_items and
self.cores == other.cores and
self.instances == other.instances and
self.injected_files == other.injected_files and
self.injected_file_content_bytes == other.injected_file_content_bytes and
self.fixed_ips == other.fixed_ips and
self.key_pairs == other.key_pairs)
class NetworkQuotas:
"""
SNAPS domain class for holding project quotas for networking services
"""
def __init__(self, **neutron_quotas):
"""
Constructor
:param neutron_quotas: the OS network quota object
"""
# Networks settings here
self.security_group = neutron_quotas['security_group']
self.security_group_rule = neutron_quotas['security_group_rule']
self.floatingip = neutron_quotas['floatingip']
self.network = neutron_quotas['network']
self.port = neutron_quotas['port']
self.router = neutron_quotas['router']
self.subnet = neutron_quotas['subnet']
def __eq__(self, other):
return (self.security_group == other.security_group and
self.security_group_rule == other.security_group_rule and
self.floatingip == other.floatingip and
self.network == other.network and
self.port == other.port and
self.router == other.router and
self.subnet == other.subnet)
|
def variableName(name):
if name[0].isdigit(): return False
for c in name:
if not c.isalnum() and not c == '_':
return False
return True |
deepspeech = True #Use Mozilla DeepSPeech
google = True #Use Google Cloud API Speech to Text
remove_wav_dir = True
data_dir = './data/'
input_dir = data_dir+'audio_examples/'
google_credentials = data_dir+ 'project_0123.json' #Replace with your json file from:
'''
https://console.cloud.google.com/apis/credentials/serviceaccountkey?_ga=2.135125617.-1992684214.1559066420
More info here:
https://cloud.google.com/docs/authentication/production
'''
# These will be created automatically:
wav_dir = input_dir[:-1]+'_wavs/'
deepspeech_models = data_dir+'deepspeech-0.5.0-models/'
output_dir = data_dir+'transcriptions/'
|
[
[
0.0036339,
0.00360887,
0.00275196,
0.0019008,
0.00174623,
0.00346563,
0.00081923,
0.00105703,
],
[
0.00380165,
0.00361068,
0.0027048,
0.00190774,
0.00170164,
0.00382391,
0.00086244,
0.00106248,
],
]
|
#5. Se tiene un monto en soles, deberá de mostrarlo en dólares.
#Conversor de cambio de divisas
Soles=float(input("Cuanto es el Valor Soles: S/ "))
Valor_Dolar=3.84
Dolar=Soles/Valor_Dolar
Dolar=round(Dolar,2)
Dolar=str(Dolar)
print("El tipo de Cambio a Dolar es $",Dolar)
|
#!/usr/bin/env python3
#https://codeforces.com/problemset/problem/1354/B
#找到最短连续子串包含123.
#规律: 一定是abbbbc形式..
#recent含义是1/2/3最近出现的位置..
def tstring(s):
counts = [s.count('1'),s.count('2'),s.count('3')]
if 0 in counts:
return 0
minlen = len(s)
recent = [None,None,None]
for i in range(len(s)):
n = int(s[i])-1
recent[n] = i
if None in recent:
continue
l = min(recent)
minlen = min(minlen,i-l+1)
return minlen
n = int(input())
[print(r) for r in [tstring(input()) for i in range(n)]]
|
__title__ = 'tes'
__description__ = 'AlfaStrakhovanie TES API Python SDK'
__url__ = ''
__version__ = '0.3.1'
__author__ = 'Sergey Popinevskiy'
__author_email__ = 'sergey.popinevskiy@gmail.com'
__license__ = 'MIT'
__copyright__ = 'Copyright 2020 Sergey Popinevskiy'
|
'''
Escreva um programa que leia dois números inteiros e compare-os. mostrando na tela uma mensagem:
- O primeiro valor é maior
- O segundo valor é maior
- Não existe valor maior, os dois são iguais
'''
n1 = int(input('Informe um número inteiro: '))
n2 = int(input('Informe mais número inteiro: '))
if n1 > n2:
print('\nO primeiro valor é MAIOR')
elif n1 < n2:
print('\nO segundo valor é MAIOR')
elif n1 == n2:
print('\nNão existe valor maior, os dois são IGUAIS')
|
inp = input('Enter a sentence: ') # Gets users input
old = 'abcdefghijklmnopqrstuvwxyz' # Lists for indexing if character is lower case
new = 'zyxwvutsrqponmlkjihgfedcba'
oldcaps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # Lists for indexing if character is uppercase
newcaps = 'ZYXWVUTSRQPONMLKJIHGFEDCBA'
for i in inp: # Iterate through code for each character in the input
if i.isupper(): # if the character is uppercase, run the code with the uppercase lists
index = oldcaps.index(i) # Gets the index from the first list
print(newcaps[index], end='') # Prints that index in the 2nd list, the opposites, and doesn't start a new line
elif i.islower(): # elif, the character is lowercase, run the code with lowercase lists
index = old.index(i) # Gets the index from the first list
print(new[index], end='') # Prints that index in the 2nd list, the opposites, and doesn't start a new line
else: # Else, it must be a special character, or space
print(i, end='') # So change nothing, print the character and don't start a new line. |
# Alpaca login info
API_URL='https://paper-api.alpaca.markets'
API_KEY=''
API_SECRET=''
# Postgres login info
DB_HOST = 'localhost'
DB_USER = 'postgres'
DB_PASS = 'password'
DB_NAME = 'stocks'
# AlphaVantage login info
ALPHA_API_KEY = ''
|
#crie um proframa que leia quanto em dineiro uma pessoa tem na carteira e mostre quantos dolares ela pode comprar
#US$ 1,00 = R$ 3,27
dinheiro = float(input('Digite quanto de dinheiro você tem? R$ '))
dolar = dinheiro / 3.27
euro = dinheiro / 4.74
iene = dinheiro / 0.039
guarani = dinheiro / 0.00067
print('O valor de R$ {} reais, voce pode comprar ate:\n USS$ {:.2f} dolares \n EUR$ {:.2f} euros \n JPY$ {:.2f} ienes \n PGY$ {:.2f} guaranis'.format(dinheiro,dolar, euro, iene, guarani)) |
"""Contains email templates for Epicast and helper functions for using them.
Email categories include:
- `alerts`: custom, one-time `notifications`-type email for a manual user group
- `notifications`: for all users, says that the CDC published new data
- `reminders`: for users with missing forecasts, a reminder that the deadline
is soon
"""
class EpicastEmails:
"""Templating for Epicast emails."""
class Template:
"""Namespace for email template text."""
# TODO: update to literal template strings after upgrade to Python 3.6+
# see https://www.python.org/dev/peps/pep-0498/
# a tag which precedes the subject in all emails
SUBJECT_TAG = '[Crowdcast]'
# the unsubscribe section embedded in all emails
UNSUBSCRIBE = {
'text': '''
----------
[This is an automated message. To edit your email preferences or to
stop receiving these emails, follow the unsubscribe link below.]
Unsubscribe: https://delphi.cmu.edu/crowdcast/preferences.php?user=%s
''',
'html': '''
<hr>
<p style="color: #666; font-size: 0.8em;">
[This is an automated message. To edit your email preferences or to
stop receiving these emails, click the unsubscribe link below.]
<br>
<a href="https://delphi.cmu.edu/crowdcast/preferences.php?user=%s">
Unsubscribe</a>
</p>
''',
}
# placeholder for one-off emails sent on special occasions
ALERT = {
'subject': 'Crowdcast Needs Your Help',
'text': '''
Dear %s,
[alert text here]
-The DELPHI Team
''',
'html': '''
<p>
Dear %s,
</p><p>
[alert text here]
</p><p>
-The DELPHI Team
</p>
''',
}
# the optional scoring section embedded in weekly notifications
SCORE = {
'text': '''
Your overall score is: %d (ranked #%d)
Note: To be listed on the leaderboards, simply enter your initials on
the preferences page at
https://delphi.cmu.edu/crowdcast/preferences.php?user=%s
You can find the leaderboards at
https://delphi.cmu.edu/crowdcast/scores.php
''',
'html': '''
<p>
Your overall score is: %d (<i>ranked #%d</i>)
<br>
Note: To be listed on the <a
href="https://delphi.cmu.edu/crowdcast/scores.php">leaderboards</a>,
simply enter your initials on the preferences page <a
href="https://delphi.cmu.edu/crowdcast/preferences.php?user=%s">
here</a>.
</p>
''',
}
# the weekly "new data available" notification
NOTIFICATION = {
'subject': 'New Data Available (Deadline: Monday 10 AM)',
'text': '''
Dear %s,
The CDC has released another week of influenza-like-illness (ILI)
surveillance data. A new round of covid19-related forecasting is now
underway, and we need your forecasts! We are asking you to please
submit your forecasts by 10:00 AM (ET) this coming Monday. Thank you so
much for your support and cooperation!
To login and submit your forecasts, visit
https://delphi.cmu.edu/crowdcast/
and enter your User ID: %s
{SCORE}
Thank you again for your participation, and good luck on your
forecasts!
Happy Forecasting!
-The DELPHI Team
''',
'html': '''
<p>
Dear %s,
</p><p>
The CDC has released another week of influenza-like-illness (ILI)
surveillance data. A new round of covid19-related forecasting is now
underway, and we need your forecasts! We are asking you to please
submit your forecasts by <b>10:00 AM (ET)</b> this coming Monday.
Thank you so much for your support and cooperation!
</p><p>
To login and submit your forecasts, click <a
href="https://delphi.cmu.edu/crowdcast/launch.php?user=%s">here</a>
or visit https://delphi.cmu.edu/crowdcast/ and enter your User ID: %s
</p>{SCORE}<p>
Thank you again for your participation, and good luck on your
forecasts!
</p><p>
Happy Forecasting!
<br>
-The DELPHI Team
</p>
''',
}
# the weekly "forecast due soon" reminder
REMINDER = {
'subject': 'Forecasts Needed (Deadline: Monday 10AM)',
'text': '''
Dear %s,
This is just a friendly reminder that your influenza-like-illness (ILI)
forecasts are due by 10:00AM (ET) on Monday. Thank you so much for your
support and cooperation!
To login and submit your forecasts, visit
https://delphi.cmu.edu/crowdcast and enter your User ID: %s.
Happy Forecasting!
-The DELPHI Team
''',
'html': '''
<p>
Dear %s,
</p><p>
This is just a friendly reminder that your influenza-like-illness
(ILI) forecasts are due by <b>10:00AM (ET) on Monday</b>. Thank you
so much for your support and cooperation!
</p><p>
To login and submit your forecasts, click <a
href="https://delphi.cmu.edu/crowdcast/launch.php?user=%s">here</a>
or visit https://delphi.cmu.edu/crowdcast/ and enter your User ID: %s
</p><p>
Happy Forecasting!
<br>
-The DELPHI Team
</p>
''',
}
@staticmethod
def prepare(text):
"""Trim surrounding whitespace and use network-style CRLF line endings."""
return '\r\n'.join([line.strip() for line in text.split('\n')]).strip()
@staticmethod
def compose(
user_id,
subject,
text_template,
html_template,
text_values,
html_values):
"""Create final subject and body from templates and values."""
final_subject = EpicastEmails.Template.SUBJECT_TAG + ' ' + subject
text_template += EpicastEmails.Template.UNSUBSCRIBE['text']
text_values += (user_id,)
final_text = EpicastEmails.prepare(text_template) % text_values
html_template += EpicastEmails.Template.UNSUBSCRIBE['html']
html_values += (user_id,)
temp_html = EpicastEmails.prepare(html_template) % html_values
final_html = '<html><body>' + temp_html + '</body></html>'
return final_subject, final_text, final_html
@staticmethod
def get_alert(user_id, user_name):
"""Fill out and return the alert email."""
template = EpicastEmails.Template.ALERT
values = (user_name,)
return EpicastEmails.compose(
user_id,
template['subject'],
template['text'],
template['html'],
values,
values)
@staticmethod
def get_notification(
user_id, user_name, last_score, last_rank, total_score, total_rank):
"""Fill out and return the notification email."""
template = EpicastEmails.Template.NOTIFICATION
subject = template['subject']
text = template['text']
html = template['html']
text_values = (user_name, user_id)
html_values = (user_name, user_id, user_id)
if last_score > 0:
# include the embedded scoring section
text = text.replace('{SCORE}', EpicastEmails.Template.SCORE['text'])
html = html.replace('{SCORE}', EpicastEmails.Template.SCORE['html'])
score_values = (total_score, total_rank, user_id)
text_values += score_values
html_values += score_values
else:
# omit the embedded scoring section
text = text.replace('{SCORE}', '')
html = html.replace('{SCORE}', '')
return EpicastEmails.compose(
user_id, subject, text, html, text_values, html_values)
@staticmethod
def get_reminder(user_id, user_name):
"""Fill out and return the reminder email."""
template = EpicastEmails.Template.REMINDER
text_values = (user_name, user_id)
html_values = (user_name, user_id, user_id)
return EpicastEmails.compose(
user_id,
template['subject'],
template['text'],
template['html'],
text_values,
html_values)
|
"""
* Assignment: Type Int Bytes
* Required: no
* Complexity: easy
* Lines of code: 7 lines
* Time: 3 min
English:
1. File size is 100 megabytes
2. Calculate size in kilobytes
2. Calculate size in megabits
3. Run doctests - all must succeed
Polish:
1. Wielkość pliku to 100 megabajtów
2. Oblicz wielkość w kilobajtach
2. Oblicz wielkość w megabitach
3. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* 1 Kb = 1024 b
* 1 Mb = 1024 Kb
* 1 B = 8 b
* 1 KB = 1024 B
* 1 MB = 1024 KB
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> assert size_kB is not Ellipsis, \
'Assign result to variable: `size_kB`'
>>> assert size_Mb is not Ellipsis, \
'Assign result to variable: `size_Mb`'
>>> assert type(size_kB) is int, \
'Variable `size_kB` has invalid type, should be int'
>>> assert type(size_Mb) is int, \
'Variable `size_Mb` has invalid type, should be int'
>>> size_kB
102400
>>> size_Mb
800
"""
b = 1
kb = 1024 * b
Mb = 1024 * kb
B = 8 * b
kB = 1024 * B
MB = 1024 * kB
SIZE = 100 * MB
# int: SIZE in kilobytes
size_kB = ...
# int: SIZE in megabits
size_Mb = ...
|
#: Progress bar of downloading torrents
def progressBar(progress):
bars = int(float(progress)) // 5
return f"{'▣'*bars}{(20-bars)*'▢'}"
#: Account space bar
def spaceBar(totalSpace, spaceUsed):
bars = round((spaceUsed / totalSpace) * 20)
return f"{'▣'*bars}{(20-bars)*'▢'}" |
# simulation nodes tree
class Tree:
def __init__(self):
# import nodes
self.imp_nds = {}
# simulation nodes
self.sim_nds = {}
# cache nodes
self.cache_nds = {}
|
# -*- coding: utf-8 -*-
# Copyright 2019 Cohesity Inc.
class EULAConfiguration(object):
"""Implementation of the 'EULA Configuration.' model.
Specifies the End User License Agreement acceptance information.
Attributes:
license_key (string): Specifies the license key.
signed_by_user (string): Specifies the login account name for the
Cohesity user who accepted the End User License Agreement.
signed_time (long|int): Specifies the time that the End User License
Agreement was accepted.
signed_version (long|int): Specifies the version of the End User
License Agreement that was accepted.
"""
# Create a mapping from Model property names to API property names
_names = {
"license_key":'licenseKey',
"signed_by_user":'signedByUser',
"signed_time":'signedTime',
"signed_version":'signedVersion'
}
def __init__(self,
license_key=None,
signed_by_user=None,
signed_time=None,
signed_version=None):
"""Constructor for the EULAConfiguration class"""
# Initialize members of the class
self.license_key = license_key
self.signed_by_user = signed_by_user
self.signed_time = signed_time
self.signed_version = signed_version
@classmethod
def from_dictionary(cls,
dictionary):
"""Creates an instance of this model from a dictionary
Args:
dictionary (dictionary): A dictionary representation of the object as
obtained from the deserialization of the server's response. The keys
MUST match property names in the API description.
Returns:
object: An instance of this structure class.
"""
if dictionary is None:
return None
# Extract variables from the dictionary
license_key = dictionary.get('licenseKey')
signed_by_user = dictionary.get('signedByUser')
signed_time = dictionary.get('signedTime')
signed_version = dictionary.get('signedVersion')
# Return an object of this model
return cls(license_key,
signed_by_user,
signed_time,
signed_version)
|
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 03 2021 - 11:55
Introduction to Computation and Programming Using Python. John V. Guttag, 2016, 2nd ed
Book Chapter 7 Exceptions and Assertions
Try, except
Finger exercise - Implement а function that meets the specification
using a try-except block
@author: Atanas Kozarev - github.com/ultraasi-atanas
"""
def sumDigits(s):
""" Assumes s is a string
Returns the sum of the decimal digits in s
For example, if s is 'a2b3c' it returns 5"""
sum = 0
letters = 0
for char in s:
try:
d = int(char) # throws ValueError: invalid literal for int() with base 10: 'a'
sum += d
except:
letters = letters + 1
# =============================================================================
# # Debug
# print(sum)
# print(letters)
#
# =============================================================================
return sum
print(sumDigits("a2b3c")) |
URL_JOB_SEARCH_HHRU = 'https://hh.ru/search/vacancy'
PARAM_HHRU_QUERY = 'text'
PARAM_HHRU_EXP = 'experience'
PARAM_HHRU_AREA = 'area'
PARAM_HHRU_PAGE = 'page'
VACANCIES_PER_PAGE = 50
|
#
# Roommate Finder v0.9
#
# This query is intended to find pairs of roommates. It almost works!
# There's something not quite right about it, though. Find and fix the bug.
#
QUERY = '''
select a.id, b.id, a.building, a.room
from residences as a, residences as b
where a.building = b.building
and a.room = b.room
and a.id != b.id
group by a.room
order by a.building, a.room;
'''
#
# To see the complete residences table, uncomment this query and press "Test Run":
#
# QUERY = "select id, building, room from residences;"
#
|
"""
Doubly Linked Lists have references to the next and previous elements
"""
class ListElement:
def __init__(self, data, prev_item=None, next_item=None):
self.data = data
self.next_item = prev_item
self.prev_item = next_item
def append(self, data):
if self.next_item:
self.next_item.append(data)
else:
self.next_item = ListElement(data, prev_item=self)
def prepend(self, data):
if self.prev_item:
self.prev_item.append(data)
else:
self.prev_item = ListElement(data, next_item=self)
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
if __name__ == '__main__':
s = input('Введите предложение:')
z = s.rfind(' ')
if z == -1:
print('Вы ввели одно слово')
exit(1)
else:
print('Последние слово:', s[z:])
exit(0)
|
# Integer
age = 22
print(age)
print(type(age))
# Float
grade = 9.8
print(grade)
print(type(grade))
# Boolean
is_published, offline = True, False
print(is_published, offline)
print(type(is_published), type(offline))
# String
movie_name = 'Nobody'
print(movie_name)
print(type(movie_name))
# List (Ordered)
mixed = [1, 2, 3.2, 4, 5.4, True, 'Hello', ['a', 'b', 'c']]
print(mixed)
print(type(mixed))
# Dictionary (python 3.7 > ordered)
movie_details = {
'name': 'Nobody',
'year': 2019,
'director': 'Me',
'cast': ['a', 'b', 'c']
}
print(movie_details)
print(type(movie_details))
# Tuple (unchangeable and ordered)
mixed_tuple = (1, 2, 3.2, 4, 5.4, True, 'Hello', ['a', 'b', 'c'])
print(mixed_tuple)
print(type(mixed_tuple))
# Set (unordered, unchangeable, unindexed)
mixed_set = {1, 2, 3.2, 4, 5.4, "Python", 'Hello'}
print(mixed_set)
print(type(mixed_set))
|
# """
# This is Sea's API interface.
# You should not implement it, or speculate about its implementation
# """
class Sea(object):
def hasShips(self, topRight: 'Point', bottomLeft: 'Point') -> bool:
pass
class Point(object):
def __init__(self, x: int, y: int):
self.x = x
self.y = y
class Solution(object):
def countShips(self, sea: 'Sea', topRight: 'Point', bottomLeft: 'Point') -> int:
top_right_x = topRight.x
top_right_y = topRight.y
bottom_left_x = bottomLeft.x
bottom_left_y = bottomLeft.y
if bottom_left_x > top_right_x or bottom_left_y > top_right_y or not sea.hasShips(topRight, bottomLeft):
return 0
if bottom_left_x == top_right_x and bottom_left_y == top_right_y:
return 1
mid_x = (bottom_left_x + top_right_x) >> 1
mid_y = (bottom_left_y + top_right_y) >> 1
blocks = [[Point(mid_x, top_right_y), Point(bottom_left_x, mid_y + 1)],
[Point(top_right_x, top_right_y), Point(mid_x + 1, mid_y + 1)],
[Point(top_right_x, mid_y), Point(mid_x + 1, bottom_left_y)],
[Point(mid_x, mid_y), Point(bottom_left_x, bottom_left_y)]]
res = 0
for block in blocks:
top_right_coord = block[0]
bottom_left_coord = block[1]
res += self.countShips(sea, top_right_coord, bottom_left_coord)
if res == 10:
return res
return res
|
"""
Copyright (c) College of Mechatronics and Control Engineering, Shenzhen University.
All rights reserved.
Description :
Author:Team Li
"""
"""
2. Add Two Numbers
Medium
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
"""
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
def addTwoNumbers(l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
## time O(n), but code not beautiful...
def sub_add(node, extra_add, l1_node, l2_node):
"""
Args:
node: a new node, which would be used to store the l1_node.val + l2_node.val + extra_add
extra_add: indicate last add whether bigger than 10
l1_node: current node of l1
l2_node: current node of l2
"""
if l1_node or l2_node:
if not l1_node:
l1_val = 0
else:
l1_val = l1_node.val
if not l2_node:
l2_val = 0
else:
l2_val = l2_node.val
node.val = (l1_val + l2_val) % 10 + extra_add
if (l1_val + l2_val) >= 10:
extra_add = 1
else:
extra_add = 0
if node.val >= 10:
node.val %= 10
extra_add = 1
if not l2_node:
if l1_node.next:
new_node = ListNode(0)
node.next = new_node
sub_add(new_node, extra_add, l1_node.next, l2_node)
elif extra_add:
new_node = ListNode(0)
node.next = new_node
sub_add(new_node, extra_add, l1_node.next, l2_node)
elif not l1_node:
if l2_node.next:
new_node = ListNode(0)
node.next = new_node
sub_add(new_node, extra_add, l1_node, l2_node.next)
elif extra_add:
new_node = ListNode(0)
node.next = new_node
sub_add(new_node, extra_add, l1_node, l2_node.next)
else:
if not l1_node.next and not l2_node.next:
if extra_add:
new_node = ListNode(0)
node.next = new_node
sub_add(new_node, extra_add, l1_node.next, l2_node.next)
else:
new_node = ListNode(0)
node.next = new_node
sub_add(new_node, extra_add, l1_node.next, l2_node.next)
elif not l1_node and not l2_node:
if extra_add:
node.val = 1
new_head = ListNode(0)
sub_add(new_head, 0, l1, l2)
return new_head |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.