1 | #include <iostream> |
Text adjust
1 | class Solution {// |
Round Point
他们公司list价格分成好几个部分,但是都是整数,如果在美金是整数,到了欧洲的网页显示汇率转换之后就变成了floating point,然后要round成整数,但是全部加起来round,和单独round再加起来,结果会不一样
base price 100 => 131.13 => 131
cleaning fee 20 => 26.23 => 26
service fee 10 => 13.54 => 14
tax 5 => 6.5 => 7.
=> 177.4E => 178E
sum 135$ => 178.93E => 179E
那么问题就来了,给个input list of floating points, 要求output list of integers, 满足以下两个constraint, 就是和跟Round(x1+x2+… +xn)的结果一样,但是minimize output 和input的绝对值差之和
Input: A = [x1, x2, …, xn]
Sum T = Round(x1+x2+… +xn) ; 178.93E => 179.
Output: B = [y1, y2, …., yn]Constraint #1: y1+y2+…+yn = T
Constraint #2: minimize sum(abs(diff(xi - yi)))
举例
A = [1.2, 2.3, 3.4]
Round(1.2 + 2.3 + 3.4) = 6.9 => 7.
1 + 2 + 3 => 61 + 3 + 3 => 7
0.2 + 0.7 + 0.4 = 1.31 + 2 + 4 => 7
0.2 + 0.3 + 0.6 = 1.1
所以[1,2,4]比[1,3,3]要好
1 | class Solution{ |
1 | #include <iostream> |
20180824
1 | //aba, aabbaa |
先装model:pip install xlwt
下面是sample code:
‘https://pypi.python.org/pypi/xlwt‘
‘’’
import xlwt
from datetime import datetime
style0 = xlwt.easyxf(‘font: name Times New Roman, color-index red, bold on’,
num_format_str=’#,##0.00’)
style1 = xlwt.easyxf(num_format_str=’D-MMM-YY’)
wb = xlwt.Workbook()
ws = wb.add_sheet(‘A Test Sheet’)
ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula(“A3+B3”))
wb.save(‘example.xls’)
‘’’
Asked to find sub-strings from a string which are valid dictionary words.
Two sentences and wanted me to return a list of words that exists in one sentence but not on the other one. for example s1 = “I am happy” s2 = “I am old”, you have to return [“happy”, “old”]
the difference betwemarken abstract class and interface
interface
An interface is a contract: the guy writing the interface says, “hey, I accept things looking that way”, and the guy using the interface says “Ok, the class I write looks that way”.
An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. The interface can’t do anything. It’s just a pattern.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25// I say all motor vehicles should look like this:
interface MotorVehicle
{
void run();
int getFuel();
}
// my team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{
int fuel;
void run()
{
print("Wrroooooooom");
}
int getFuel()
{
return this.fuel;
}
}
Implementing an interface consumes very little CPU, because it’s not a class, just a bunch of names, and therefore there is no expensive look-up to do. It’s great when it matters such as in embedded devices.
Abstract class
Abstract classes, unlike interfaces, are classes. They are more expensive to use because there is a look-up to do when you inherit from them.
Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It’s more about a guy saying, “these classes should look like that, and they have that in common, so fill in the blanks!”.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28// I say all motor vehicles should look like this :
abstract class MotorVehicle
{
int fuel;
// they ALL have fuel, so why not let others implement this?
// let's make it for everybody
int getFuel()
{
return this.fuel;
}
// that can be very different, force them to provide their
// implementation
abstract void run();
}
// my team mate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
void run()
{
print("Wrroooooooom");
}
}
Implementation
While abstract classes and interfaces are supposed to be different concepts, the implementations make that statement sometimes untrue. Sometimes, they are not even what you think they are.
In Java, this rule is strongly enforced, while in PHP, interfaces are abstract classes with no method declared.
In Python, abstract classes are more a programming trick you can get from the ABC module and is actually using metaclasses, and therefore classes. And interfaces are more related to duck typing in this language and it’s a mix between conventions and special methods that call descriptors (the method methods).
As usual with programming, there is theory, practice, and practice in another language :-)
转载:https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html
C++ Programming Language
Object-Oriented Programming (OOP) in C++
Why OOP?
Suppose that you want to assemble your own PC, you go to a hardware store and pick up a motherboard, a processor, some RAMs, a hard disk, a casing, a power supply, and put them together. You turn on the power, and the PC runs. You need not worry whether the motherboard is a 4-layer or 6-layer board, whether the hard disk has 4 or 6 plates; 3 inches or 5 inches in diameter, whether the RAM is made in Japan or Korea, and so on. You simply put the hardware components together and expect the machine to run. Of course, you have to make sure that you have the correct interfaces, i.e., you pick an IDE hard disk rather than a SCSI hard disk, if your motherboard supports only IDE; you have to select RAMs with the correct speed rating, and so on. Nevertheless, it is not difficult to set up a machine from hardware components.
Similarly, a car is assembled from parts and components, such as chassis, doors, engine, wheels, brake, and transmission. The components are reusable, e.g., a wheel can be used in many cars (of the same specifications).
Hardware, such as computers and cars, are assembled from parts, which are reusable components.
How about software? Can you “assemble” a software application by picking a routine here, a routine there, and expect the program to run? The answer is obviously no! Unlike hardware, it is very difficult to “assemble” an application from software components. Since the advent of computer 60 years ago, we have written tons and tons of programs. However, for each new application, we have to re-invent the wheels and write the program from scratch.
Why re-invent the wheels?
简写t
- 简写替换:如果db=test, collection=profile, 可以简写t=db.profile, 以后t就可以代替db.profile了
mongoimport
1
> mongoimport -d DBName -c CollectionName FileName
bool 不用加引号(EX: {“registered”:false})
Insert
db.
Full update
1 | db.<collection>.update(<where>,<full or patial update expression>[,<upsert>,<multi>]) |
mongo shell update use javascript
myobj=t.findOne() 存现有值到一个myobj变量里, 注意一定要用findOne()
- t.update({_id : myobj._id}, myobj)
- t.save(myobj)
From: http://www.acunetix.com/websitesecurity/csrf-attacks/
CSRF Attacks, XSRF or Sea-Surf – What They Are and How to Defend Against Them
Cross-Site Request Forgery, or CSRF for short is a common and regular online attack. CSRF also goes by the acronym XSRF and the phrase “Sea-Surf”. CSRF attacks include a malicious exploit of a website in which a user will transmit malicious requests that the target website trusts without the user’s consent. In Cross-Site Scripting (XSS), the attacker exploits the trust a user has for a website, with CSRF on the other hand, the attacker exploits the trust a website has against a user’s browser.
Basically, an attacker will use CSRF to trick a victim into accessing a website or clicking a URL link that contains malicious or unauthorized requests. It is called ‘malicious’ since the CSRF attack will use the identity and privileges of the victim and impersonate them in order to perform any actions desired by the attacker, such as change form submission details, and launch purchases or payments for the attacker or a third-party account.
Python dict的定义方法:1
2
3
4
5a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})